根据textarea行调整TD高度 [英] Adjust TD height depending on textarea rows
问题描述
我在< td>
内有 rows =1
的textarea,所以它占用了空的时候尽可能少的空间。
I have a textarea with rows="1"
inside a <td>
, so it takes up as little space as possible when empty.
现在我想知道,当用户按下回车键时,我最好如何扩展textarea?
Now I was wondering, how would I best "expand" the textarea when the user presses the enter key?
我已经提出了一个非常简单的 jsfiddle 来测试这个想法。不幸的是我对jsfiddle并不是很好,所以我不知道如何(或者是否)可以使用 .on()
或其他事件监听器,所以我只是提出了一个在jsfiddle运行时运行的一次性更新。
I have put up a very simple jsfiddle to test the idea. Unfortunately I am not very good with jsfiddle, so I did not know how (or if) is it possible to use .on()
or other event listeners, so I have simply put up a one-time update that runs when the jsfiddle is ran.
到目前为止它有效,但我想知道是否有更好/更有效的方法来实现它。
So far it works, but I was wondering if there's a better/more efficient way to do it.
PS调用我想要使用的函数 keypress
然后这段代码找到其他
PS to call the function I was thinking to use keypress
and then this code found here on SO
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
//expand()
}
推荐答案
试试这个:
$('.expand').on('keypress', function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
// Enter pressed... do anything here...
var rows = $(this).attr('rows');
var rowsNew = parseInt(rows) + 1;
$(this).attr('rows', rowsNew);
}
});
这篇关于根据textarea行调整TD高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!