Adjust TD height depending on textarea rows

百般思念 提交于 2019-12-10 21:38:13

问题


I have a textarea with rows="1" inside a <td>, so it takes up as little space as possible when empty.

Now I was wondering, how would I best "expand" the textarea when the user presses the enter key?

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 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()
}

回答1:


Try this:

$('.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);
    }
});

DEMO HERE



来源:https://stackoverflow.com/questions/16102880/adjust-td-height-depending-on-textarea-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!