1

高さを自動的に増やしたいテキストエリアがありますが、機能しません。これが私のjQueryです。

$('.flat_textarea').delegate( 'textarea', 'keyup', function (){
   $(this).height( 30 );
   if(this.scrollHeight>30) 
 $(this).height(this.scrollHeight);
});

$('.flat_textarea').find( 'textarea' ).keyup();

$('.flat_textarea textarea').on("keyup",function (){
  $(this).height( 30 );
  if(this.scrollHeight>30) 
    $(this).height(this.scrollHeight);
});

HTML:

<form method="POST" class="flat_textarea" >
    <textarea></textarea>
</form>
4

1 に答える 1

1

delegate() の代わりにon()を使用します(delegate() は非推奨です) - そしてkeyup() の代わりにkeypress()を使用します。

これは動作中のjsFiddleです。

コードを次のように変更します。

$('.flat_textarea').on('keypress', 'textarea', function (){
   $(this).height(30);
   if(this.scrollHeight > 30) 
     $(this).height(this.scrollHeight);
});

$('.flat_textarea').find('textarea').keypress();

$('.flat_textarea textarea').on("keypress", function (){
  $(this).height(30);
  if(this.scrollHeight > 30) 
    $(this).height(this.scrollHeight);
});
于 2013-04-07T12:21:56.783 に答える