在文本框的焦点上设置光标长度为 14
大家好,我想在一个没有值的文本框上将光标设置在长度为 14 的位置.我知道最初光标将在 0 我希望它在 14
Hai Guys, I want to set cursor at a position of length 14 on a textbox which will not have a value.. Iknow initially cursor will be at 0 i want it to be at 14
推荐答案
IE 在设置光标位置时使用了与 Firefox、Opera 和 Chrome 不同的方法.最好制作一个辅助函数,它会为你做这件事.我用这个来满足自己的需要.
IE use different approach at setting cursor position than Firefox,Opera and Chrome. It's better to make a helper function, which will do it for you. I use this one for own needs.
function setCursor(node,pos){
node = (typeof node == "string" || node instanceof String) ? document.getElementById(node) : node;
if(!node){
return false;
}else if(node.createTextRange){
var textRange = node.createTextRange();
textRange.collapse(true);
textRange.moveEnd(pos);
textRange.moveStart(pos);
textRange.select();
return true;
}else if(node.setSelectionRange){
node.setSelectionRange(pos,pos);
return true;
}
return false;
}
最后一件事是从您的 onfocus 处理程序中调用它.
Last thing, is to call it from your onfocus handler.
祝你好运
相关文章