html部分:
<textarea id="textarea">哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽</textarea>
js部分:
<!--textarea高度跟随文字高度而变化-->
<script>
function makeExpandingArea(el) {
var setStyle = function(el) {
el.style.height = 'auto';
el.style.height = el.scrollHeight + 'px';
// console.log(el.scrollHeight);
}
var delayedResize = function(el) {
window.setTimeout(function() {
setStyle(el)
},
0);
}
if (el.addEventListener) {
el.addEventListener('input',function() {
setStyle(el)
},false);
setStyle(el)
} else if (el.attachEvent) {
el.attachEvent('onpropertychange',function() {
setStyle(el)
});
setStyle(el)
}
if (window.VBArray && window.addEventListener) { //IE9
el.attachEvent("onkeydown",function() {
var key = window.event.keyCode;
if (key == 8 || key == 46) delayedResize(el);
});
el.attachEvent("oncut",function() {
delayedResize(el);
}); //处理粘贴
}
}
makeExpandingArea(textarea);
</script>