JS_textarea自適應高度


/*
* IE6,7,8使用onpropertychange就能實現自適應;
* FF,Chrom,opera,讓textarea恢復原高度讓他獲取ScrollHeight的正確高度;
* IE9,退格鍵oninput,onpropertychange都觸發不了回調函數,所以綁定個onkeyup;

*/
<
style type="text/css"> .box{padding-top:50px;text-align:center;margin-bottom:20px;} .main{width:200px;height:48px;border:1px solid #CCC;resize:none;font:12px/16px Arial;color:#CCC;overflow:hidden;} </style> <div class="box"> <textarea class="main" id="main"></textarea> </div> <script type="text/javascript"> /*----外置函數star----*/ var ie = !!window.attachEvent && !window.opera; var ie9 = ie && (!!+"\v1"); var inputhandler = function(node,fun){ if("oninput" in node){ node.oninput = fun; }else{ node.onpropertychange = fun; } if(ie9) node.onkeyup = fun; } /*----外置函數end---*/ var main = document.getElementById("main"); inputhandler(main,function(){ if(!ie) main.style.height = 48+"px"; var height = main.scrollHeight;if(height>=48){ main.style.height = height+"px"; }else{ main.style.height = 48+"px"; } }); </script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM