사용자가 편집 할 내용으로 텍스트 영역을 채우고 있습니다.
overflow:showdiv 와 같은 CSS로 콘텐츠에 맞게 늘릴 수 있습니까?
답변:
한 줄만
<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>기본적으로 먼저 높이를 1px로 설정해야합니다.이 때문에 stackoverflow.com/questions/10722058/…