Heim >Web-Frontend >js-Tutorial >往光标所在位置插入值的js代码_javascript技巧
/** <br>*往输入域中插入字符串(光标所在位置) <br>*@param $t document.getElementById('fieldId') <br>*@param myValue 要插入的值 <br>** <br>function addSplitToField($t,myValue){ <br>if (document.selection) { <br>$t.focus(); <br>sel = document.selection.createRange(); <br>sel.text = myValue; <br>$t.focus(); <br>}else if($t.selectionStart || $t.selectionStart == '0') { <br>var startPos = $t.selectionStart; <br>var endPos = $t.selectionEnd; <br>var scrollTop = $t.scrollTop; <br>$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length); <br>this.focus(); <br>$t.selectionStart = startPos + myValue.length; <br>$t.selectionEnd = startPos + myValue.length; <br>$t.scrollTop = scrollTop; <br>}else{ <br>$t.value += myValue; <br>$t.focus(); <br>} <br>} <br>