往光标所在位置插入值的js代码

时间:2013-09-22    点击:91   
复制代码 代码如下:

<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>
JS加jquery简单实现标签元素的显示或隐藏
JQuery操作单选按钮以及复选按钮示例
通过AJAX的JS、JQuery两种方式解析XML示例介绍
js如何取消事件冒泡
页面载入结束自动调用js函数示例
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved