javascript - js怎么獲取textarea自動換行行數(shù)?
問題描述
要在textarea中輸入內(nèi)容,默認(rèn)為一行,當(dāng)輸入的內(nèi)容過多時自動換行,并且高度增加?
問題解答
回答1:1, 用shadow
<p style='height:0; overflow:hidden;'> <p class='shadow'></p></p><textarea style='overflow:hidden;'></textarea><script> textarea.addEventListener(’input’, function(e) {shadow.innerHTML = this.value.replace(/</g, ’<’).replace(/>/g, ’>’);this.height = shadow.clientHeight + ’px’; });</script>
2, 用 contenteditable 屬性
<p contenteditable='true'>這里的高度會隨內(nèi)容自動擴展</p>
3, 如果用
textarea.style.height = textarea.scrollHeight + ’px’;
這種形式也可以調(diào)整高度, 但換行的時候會有滾動條一閃而逝, 而且高度只會增加沒法減少, 是體驗最差的寫法.
回答2:給textarea一個oninput事件
<textarea id='text'></textarea>document.getElementById(’text’).style.height = document.getElementById(’text’).scrollHeight + ’px’
類似這樣的
回答3:http://stackoverflow.com/ques...
回答4:textarea的總高度(用jQ的element.height(),如果是原生js,請查看手冊的BIF)/ 你定義的行高
相關(guān)文章:
1. boot2docker無法啟動2. docker-compose中volumes的問題3. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””4. docker容器呢SSH為什么連不通呢?5. java - SSH框架中寫分頁時service層中不能注入分頁類6. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.7. 關(guān)于docker下的nginx壓力測試8. node.js - antdesign怎么集合react-redux對input控件進(jìn)行初始化賦值9. dockerfile - 為什么docker容器啟動不了?10. nignx - docker內(nèi)nginx 80端口被占用

網(wǎng)公網(wǎng)安備