textarea高度隨文本內容變化,且不出現滾動條

Html代碼部分:

<div class= "con-info order-info-con1 ">
    <p class="con-tit con-se-tit con-tit-items">備註: </p>
        <div class="con-msg order-comment-div">
            <textarea class="comment-textarea" rows="" cols="" th:text="${cretInfo.cComment}" disabled="disabled"></textarea>
        </div>
 </div>

Css代碼部分:

.info-textarea {
        border: 0px;
        background-color: white;
        width: 514px;
        height: 125px;
        resize: none;
        text-indent: 25px;
}

JavaScript代碼部分:

//textarea高度隨文本內容變化,且不出現滾動條
$('textarea').each(function () {
    this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;');
}).on('input', function () {
    this.style.height = 'auto';
    this.style.height = (this.scrollHeight) + 'px';
});

最終效果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章