将光标设置为文本框的焦点长度14 [英] Set cursor at a length of 14 onfocus of a textbox

查看:117
本文介绍了将光标设置为文本框的焦点长度14的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai Guys,
我想将光标设置在文本框上的长度为14的位置上,该位置不会有值..我知道最初的光标位于0,我希望它位于14

解决方案

IE使用不同的方法来设置光标位置,而不是Firefox,Opera和Chrome。做一个辅助函数会更好,它会为你做。

$ p $ 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();
返回true;
} else if(node.setSelectionRange){
node.setSelectionRange(pos,pos);
返回true;
}

return false;
}

最后一件事就是从onfocus处理程序调用它。



Goodluck

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 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;
}

Last thing, is to call it from your onfocus handler.

Goodluck

这篇关于将光标设置为文本框的焦点长度14的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆