当前位置: 代码迷 >> Web前端 >> 文本域中焦点处安插字符串
  详细解决方案

文本域中焦点处安插字符串

热度:76   发布时间:2013-01-26 13:47:02.0
文本域中焦点处插入字符串
el代表文本域对象,myvalue代表要录入的字符串。只适用于文本域中焦点追加字符串

function insertAtCursor(el, myValue) {

    //IE support 
    if (document.selection) { 
        el.focus(); 
        sel = document.selection.createRange(); 
        sel.text = myValue; 
    } else if (el.selectionStart || el.selectionStart == '0') {
        //MOZILLA/NETSCAPE support 
        var startPos = el.selectionStart; 
        var endPos = el.selectionEnd; 
        el.value = el.value.substring(0, startPos) + myValue + el.value.substring(endPos, el.value.length); 
    } else { 
        el.value += myValue; 
    } 
}


  相关解决方案