在textarea中按下tab键时使用tab-indent但是它将转到html中的下一个元素

时间:2022-05-12 19:42:52
<textarea></textarea>

This is the code i just need an output in which whenever tab is placed it indent to right.. but pressing tab do something else

这是我只需要一个输出的代码,每当选项卡被放置时它向右缩进..但是按Tab键会做其他事情

1 个解决方案

#1


1  

Please see the following example: http://www.jqversion.com/#!/liDxmDg

请参阅以下示例:http://www.jqversion.com/#!/ liDxmDg

you can use a jQuery code to accomplish that:

您可以使用jQuery代码来实现:

$(document).delegate('textarea', 'keydown', function(e) {
  var keyCode = e.keyCode || e.which;

  if (keyCode == 9) {
    e.preventDefault();
    var start = $(this).get(0).selectionStart;
    var end = $(this).get(0).selectionEnd;

    // set textarea value to: text before caret + tab + text after caret
    $(this).val($(this).val().substring(0, start)
                + "\t"
                + $(this).val().substring(end));

    // put caret at right position again
    $(this).get(0).selectionStart =
    $(this).get(0).selectionEnd = start + 1;
  }
});

#1


1  

Please see the following example: http://www.jqversion.com/#!/liDxmDg

请参阅以下示例:http://www.jqversion.com/#!/ liDxmDg

you can use a jQuery code to accomplish that:

您可以使用jQuery代码来实现:

$(document).delegate('textarea', 'keydown', function(e) {
  var keyCode = e.keyCode || e.which;

  if (keyCode == 9) {
    e.preventDefault();
    var start = $(this).get(0).selectionStart;
    var end = $(this).get(0).selectionEnd;

    // set textarea value to: text before caret + tab + text after caret
    $(this).val($(this).val().substring(0, start)
                + "\t"
                + $(this).val().substring(end));

    // put caret at right position again
    $(this).get(0).selectionStart =
    $(this).get(0).selectionEnd = start + 1;
  }
});