点击鼠标在textarea中光标当前位置插入指定字符

来源:互联网 发布:手机能编程么 编辑:程序博客网 时间:2024/05/16 23:37
 function insertText(obj,str) {   //obj为textarea 元素,str为要插入字符    if(Switch == 1){    if (document.selection) {        var sel = document.selection.createRange();        sel.text = str;        setcopy(0);    } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {        var startPos = obj.selectionStart;        var    endPos = obj.selectionEnd;        var    cursorPos = startPos;        var    tmpStr = obj.value;        obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);        cursorPos += str.length;        obj.selectionStart = obj.selectionEnd = cursorPos;        setcopy(0);    } else {        obj.value += str;        setcopy(0);    }    }}

原创粉丝点击