日期:2014-05-16 浏览次数:20397 次
原文:http://www.js8.in/466.html
PS:参数ctrl为input或者textarea对象,pos为光标要移动到的位置
?
function getCursortPosition (ctrl) {//获取光标位置函数
var CaretPos = 0; // IE Support
if (document.selection) {
ctrl.focus ();
var Sel = document.selection.createRange ();
Sel.moveStart ('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart;
return (CaretPos);
}
function setCaretPosition(ctrl, pos){//设置光标位置函数
if(ctrl.setSelectionRange)
{
ctrl.focus()