注册 X
我已阅读并同意  服务条款
首页 > IT技术笔记 > 查看笔记

使textarea支持tab缩进

使textarea支持tab缩进

点击复制

<textarea onblur="gettextData(this)" onkeydown="tab(this)"></textarea>
<javascript>
function insertAtCursor(obj, txt) {
    obj.focus();
    //IE support
    if (document.selection) {
        sel = document.selection.createRange();
        sel.text = txt;
    }
    //MOZILLA/NETSCAPE support
    else {
        var startPos = obj.selectionStart;
        var scrollTop = obj.scrollTop;
        var endPos = obj.selectionEnd;
        obj.value = obj.value.substring(0, startPos) + txt + obj.value.substring(endPos, obj.value.length);
        startPos += txt.length;
        obj.setSelectionRange(startPos, startPos);
        obj.scrollTop = scrollTop;
    }
}
function tab(obj){
	  if (event.keyCode == 9){
		  event.preventDefault();
          insertAtCursor(obj, "    ");	//增加几格
          obj.returnValue = false;
	  }
}
<javascript>

 打赏        分享



评论