直接看程式碼吧,很簡單,也很實用。 複製程式碼 代碼如下: 🎜>autoresizing textarea <BR>textarea {<BR> border ;<BR> padding: 0;<BR> outline: none;<BR> background-color: #D0D0D0;<BR> resize:color: #D0D0D0;<BR> "text/javascript"><BR>var observe;<BR>if (window.attachEvent) {<BR> observe = function (element, event, handler) {<BR> .). ;<BR> };<BR>}<BR>else {<BR> observe = function (element, event, handler) {<BR> >}<BR>function init () {<BR> var text = document.getElementById('text');<BR> function resize () {< text .style.height = text.scrollHeight 'px';<BR> }<BR> /* 0-timeout to get the already changed text */<BR> function delayedResize () {<BR> window.setTimeout(resize, 0 );<BR> }<BR> observe(text, 'change', resize);<BR> observe(text, 'cut', 🎜 > observe(text, 'drop', delayedResize);<BR> observe(text, 'keydown', delayedResize);<BR> <BR> text.focus();<BR> text.select();<BR> resize();<BR>}<BR><BR>}<P><BR>< "init();"><BR><textarea cols="40" rows="1" style="height:25px;" id="text"><BR>