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

Jos*_*h R 20 javascript jquery textarea javascript-events

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

Vin*_*nie 29

你可以尝试这样的事情:

     $(document).ready(function(){

    $('#moo').focus(function(){
        $(this).attr('rows', '4');
    });
});
Run Code Online (Sandbox Code Playgroud)

moo是你的textarea.


Phr*_*ogz 6

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

或者,使用更少的jQuery,更少的打字,并获得更多的性能:

jQuery(function($){
  $('#foo')
    .focus(function(){ this.rows=5 })
    .blur( function(){ this.rows=1 });
});
Run Code Online (Sandbox Code Playgroud)