2012-10-31 333 views
1

我想自动调整textarea的宽度,当我写入它时,使用最大宽度,就像这段代码对输入做的那样。 http://jsbin.com/ahaxe 有可能吗?Textarea宽度自动调整大小

+0

也许在你的wisth中使用em。它表示M.的宽度不会很精确,但它可以工作。 –

+0

Duplicate:http://stackoverflow.com/questions/2948230/auto-expand-textarea –

回答

2

DEMO:http://jsfiddle.net/buM6M/1/

HTML:

<textarea id="t1" style='min-width:100px;min-height: 100px'> 

</textarea> 

<div id="d1" style='max-width:200px;min-height:20px;display: none'> 

</div> 

JS代码:

$("#t1").live({ 
keyup: function(e){ 

    $("#d1").text($(this).val()); 
    $(this).css("width", $("#d1").css("width")); 
    $(this).css("height", $("#d1").css("height")); 
} 
});​ 

设置div和textarea的尺寸按照您的要求。

0

使用jquery autogrow插件。

下载autogrowtextarea.js并添加脚本

,并在头部添加这个CSS:

#id_of_textarea 
{ 
height:auto; 
width:width_of_textarea_you_want 
} 


$(function() { 
$('#id_of_textarea').autogrow(); 
}); 

避免滚动:

$('#id_of_textarea').css('overflow', 'hidden').autogrow() 
+0

你从哪里采取'autogrow'? – Dementic

+1

它的一个plugin.i已经更新了我的答案。 –

相关问题