飘凌大杂烩编程技术 → javascript 文本区光标插入值
查看完整版本:javascript 文本区光标插入值
2008/9/25 18:15:45

1.取得文本框中鼠标的位置
function test(obj)
{
obj.focus();
var workRange=document.selection.createRange();
var allRange=obj.createTextRange();
workRange.setEndPoint("StartToStart",allRange);
len=workRange.text.length;
alert(len);
}

2.取得文本区中选中的字符

function test(obj)
{
obj.focus();
var sel=document.selection.createRange();
alert(sel.text);
}

3.取得文本区滚动条的位置

textarea.scrollTop

4.在文本区光标所在位置插入字符

function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
} else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}

Powered by BBSXP 2007 ACCESS © 1998-2025
Processed in 0.01 second(s)