往光標(biāo)所在位置插入值的js代碼
更新時間:2013年09月22日 17:45:03 作者:
往輸入域中插入字符串(光標(biāo)所在位置),在本文將為大家介紹下使用js是如何實現(xiàn)的,感興趣的朋友可以參考下
復(fù)制代碼 代碼如下:
<pre name="code" class="javascript">/**
*往輸入域中插入字符串(光標(biāo)所在位置)
*@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();
}
}
</pre><br><br>
相關(guān)文章
js將當(dāng)前時間格式化為 年-月-日 時:分:秒的實現(xiàn)代碼
這篇文章主要介紹了js將當(dāng)前時間格式化為 年-月-日 時:分:秒主要是使用js的Date()對象,將系統(tǒng)當(dāng)前時間格式化為年-月-日 時:分:秒,需要的朋友可以參考下2018-01-01
淺析JavaScript中的同名標(biāo)識符優(yōu)先級
這篇文章主要介紹了JavaScript中的同名標(biāo)識符優(yōu)先級。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
使用javascript函數(shù)編寫簡單銀行取錢存錢流程
本文通過實例代碼給大家講解了使用javascript函數(shù)編寫簡單銀行取錢存錢流程,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05

