javascript代码实例教程-JS在TextArea光标位置插入文字+移动光标到文字末尾

发布时间:2019-04-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-JS在TextArea光标位置插入文字+移动光标到文字末尾脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

=IE支持document.selection
=Firefox,Chrome,Safari以及opera都有selectionStart和selectionEnd属性

 

 

[javascript]
function insertText(obj,str) { 
 
    if (document.selection) { 
 
        VAR sel = document.selection.createRange(); 
 
        sel.text = str; 
 
    } else if (tyPEof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') { 
 
        var startPos = obj.selectionStart, 
 
            endPos = obj.selectionEnd, 
 
            cursorPos = startPos, 
 
            tmpStr = obj.value; 
 
        obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length); 
 
        cursorPos += str.length; 
 
        obj.selectionStart = obj.selectionEnd = cursorPos; 
 
    } else { 
 
        obj.value += str; 
 
    } 
 

 
function moveEnd(obj){ 
 
    obj.focus(); 
 
    var len = obj.value.length; 
 
    if (document.selection) { 
 
        var sel = obj.createTextRange(); 
 
        sel.moveStart('character',len); 
 
        sel.collapse(); 
 
        sel.select(); 
 
    } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') { 
 
        obj.selectionStart = obj.selectionEnd = len; 
 
    } 
 
 

function insertText(obj,str) {

    if (document.selection) {

        var sel = document.selection.createRange();

        sel.text = str;

    } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {

        var startPos = obj.selectionStart,

            endPos = obj.selectionEnd,

            cursorPos = startPos,

            tmpStr = obj.value;

        obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);

        cursorPos += str.length;

        obj.selectionStart = obj.selectionEnd = cursorPos;

    } else {

        obj.value += str;

    }

}

function moveEnd(obj){

    obj.focus();

    var len = obj.value.length;

    if (document.selection) {

        var sel = obj.createTextRange();

        sel.moveStart('character',len);

        sel.collapse();

        sel.select();

    } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {

        obj.selectionStart = obj.selectionEnd = len;

    }


}[javascript]
<input type="button" onclick="insertText(document.getElementById('text'),' 新文字—YoyiorLee ')" value="插入文字"></input> 

<input type="button" onclick="insertText(document.getElementById('text'),' 新文字—YoyiorLee ')" value="插入文字"></input>

 


[javascript]
<input type="button" onclick=";moveEnd(document.getElementById('text'))" value="移到末尾"></input> 

<input type="button" onclick="moveEnd(document.getElementById('text'))" value="移到末尾"></input>


 

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-JS在TextArea光标位置插入文字+移动光标到文字末尾全部内容,希望文章能够帮你解决javascript代码实例教程-JS在TextArea光标位置插入文字+移动光标到文字末尾所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。