ユーザーが編集できるコンテンツをテキスト領域に入力しています。
(overflow:show
divのように)CSSでコンテンツに合うようにストレッチすることは可能ですか?
ユーザーが編集できるコンテンツをテキスト領域に入力しています。
(overflow:show
divのように)CSSでコンテンツに合うようにストレッチすることは可能ですか?
回答:
1行のみ
<textarea name="text" oninput='this.style.height = "";this.style.height = this.scrollHeight + "px"'></textarea>
this.scrollHeight + "px"
とするとthis.scrollHeight + 3 + "px"
、それはスクロールバーを表示しない持っているテキストエリアに大きな十分になります。
jQueryで機能する関数は次のとおりです(幅ではなく高さのみ)。
function setHeight(jq_in){
jq_in.each(function(index, elem){
// This line will work with pure Javascript (taken from NicB's answer):
elem.style.height = elem.scrollHeight+'px';
});
}
setHeight($('<put selector here>'));
注: opは、Javascriptを使用しないソリューションを求めましたが、これは、この質問に出くわした多くの人々に役立つはずです。
これは非常に単純な解決策ですが、私にとってはうまくいきます。
<!--TEXT-AREA-->
<textarea id="textBox1" name="content" TextMode="MultiLine" onkeyup="setHeight('textBox1');" onkeydown="setHeight('textBox1');">Hello World</textarea>
<!--JAVASCRIPT-->
<script type="text/javascript">
function setHeight(fieldId){
document.getElementById(fieldId).style.height = document.getElementById(fieldId).scrollHeight+'px';
}
setHeight('textBox1');
</script>
<style> textarea { resize: none; overflow: auto; } </style> <!--TEXT-AREA--> <textarea onkeyup="setHeight.call(this);">Hello World</textarea> <!--JAVASCRIPT--> <script type="text/javascript"> function setHeight(){ this.style.height = '1px'; this.style.height = this.scrollHeight + 'px'; } </script>
基本的にはこれにより、最初の1ピクセルまでの高さを設定する必要があります。stackoverflow.com/questions/10722058/...