Javascript 如何使用jQuery更改textarea中的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4417161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:20:00  来源:igfitidea点击:

How to change the number of rows in the textarea using jQuery

javascriptjqueryjavascript-eventstextarea

提问by Josh R

I have a textarea with 5 lines. I want to show only one line and on focus it should show remaining 4 lines.

我有一个 5 行的 textarea。我只想显示一行,在焦点上它应该显示剩余的 4 行。

回答by Vincent Ramdhanie

You can try something like this:

你可以尝试这样的事情:

     $(document).ready(function(){

    $('#moo').focus(function(){
        $(this).attr('rows', '4');
    });
});

where moo is your textarea.

其中 moo 是您的 textarea。

回答by Phrogz

jQuery(function($){
  $('#foo').focus(function(){
    $(this).attr('rows',5);
  }).blur(function(){
    $(this).attr('rows',1);
  });
});

Or, using less jQuery, less typing, and getting a hair more performance:

或者,使用更少的 jQuery,更少的输入,并获得更高的性能:

jQuery(function($){
  $('#foo')
    .focus(function(){ this.rows=5 })
    .blur( function(){ this.rows=1 });
});

回答by Vish Kamath

Try this

尝试这个

$('#textboxid').focus(function()
    {
       $(this).animate({'height': '185px'}, 'slow' );//Expand the textarea on clicking on it
       return false;
     });