我正在尝试根据从输入文本区域中反映出的文本长度自动调整文本区域的高度。您能否看一下可能存在的问题?谢谢您的帮助。
http://jsfiddle.net/wshuni/vjwe39ah/93/
var titleInput = document.querySelector("#titleInput");
var bodyInput = document.querySelector("#bodyInput");
var title = document.querySelector("#title");
var body = document.querySelector("#body");
title.addEventListener('keydown', autosize);
body.addEventListener('keydown', autosize);
titleInput.addEventListener('keydown', autosize);
bodyInput.addEventListener('keydown', autosize);
function autosize(){
var el = this;
setTimeout(function(){
el.style.cssText = 'height:auto; padding:0';
// for box-sizing other than "content-box" use:
// el.style.cssText = '-moz-box-sizing:content-box';
el.style.cssText = 'height:' + el.scrollHeight + 'px';
},0);
}
var input = document.querySelector("#titleInput");
var mirror = document.querySelector("#title");
var bodyInput = document.querySelector("#bodyInput");
var bodyMirror = document.querySelector("#body");
input.addEventListener('input', function(event) {
mirror.innerText = event.target.value;
});
bodyInput.addEventListener('input', function(event) {
bodyMirror.innerText = event.target.value;
});
{{链接1:解释 - 图像}}