DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 往光標所在位置插入值的js代碼
往光標所在位置插入值的js代碼
編輯:關於JavaScript     
復制代碼 代碼如下:
<pre name="code" class="javascript">/**
*往輸入域中插入字符串(光標所在位置)
*@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>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved