【发布时间】:2011-12-01 12:58:37
【问题描述】:
我正在实现一个自动文本区域大小调整器,当用户按 ENTER 并在最小 scrollHeight 大小之后自动扩展文本区域。
它在 IE、Firefox 和 Opera 中运行良好,但在 Chrome 中不运行。
在 Chrome 中,文本区域在任何按键时都会调整大小(不仅是 ENTER),因为在设置 e.style.height 时它也会更改 scrollHeight(这不会发生在其他浏览器上。
我用注释标记了有问题的行:
function resize(e)
{
e = e.target || e;
var $e = $(e);
console.log( 'scrollHeight:'+e.scrollHeight );
console.log( 'style.height:'+e.style.height );
var h = Math.min( e.scrollHeight, $e.data('textexp-max') );
h = Math.max( h, $e.data('textexp-min') );
if( $e.data('h')!=h )
{
e.style.height = "";
e.style.height = h + "px"; //this changes the scrollHeight even if style.height is smaller thatn scrollHeight
//e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden");
$e.data('h',h);
}
return true;
}
在这里您可以找到完整代码:http://jsfiddle.net/NzTYd/9/
更新
我发现当我在 Firefox/IE/Opera 中更新 e.style.height 时,e.scrollHeight 会更新为完全相同的值:
例子:
设置80px为e.style.height,设置为80pxe.scrollHeight
但是,Chrome 将 e.scrollHeight 更新为更高的值(多 4 像素)。
例子::
设置80px为e.style.height,设置为84pxe.scrollHeight
这会导致每收到一次按键,textarea 就会增长 4px!
【问题讨论】:
标签: javascript jquery google-chrome