MENU▼

[JQuery]Textarea标签中使用Tab缩进

JQuery:

$("textarea").on('keydown',function(e){
    if(e.keyCode == 9){
        e.preventDefault();
        var indent = '\t';//可以换成空格
        var start = this.selectionStart;
        var end = this.selectionEnd;
        var selected = window.getSelection().toString();
        selected = indent + selected.replace(/\n/g,'\n'+indent);
        this.value = this.value.substring(0,start) + selected + this.value.substring(end);
        this.setSelectionRange(start+indent.length,start+selected.length);
    }
});

请为这篇文章评分:
( 这篇文章尚未评分 )