往光标所在位置插入值的js代码_javascript技巧
来源:动视网
责编:小采
时间:2020-11-27 21:13:58
往光标所在位置插入值的js代码_javascript技巧
往光标所在位置插入值的js代码_javascript技巧: 代码如下: /** *往输入域中插入字符串(光标所在位置) *@param $t document.getElementById('fieldId') *@param myValue 要插入的值 ** function addSplitToField($t,myValue){ if (document.selection) { $
导读往光标所在位置插入值的js代码_javascript技巧: 代码如下: /** *往输入域中插入字符串(光标所在位置) *@param $t document.getElementById('fieldId') *@param myValue 要插入的值 ** function addSplitToField($t,myValue){ if (document.selection) { $

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