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

查看:20
本文介绍了在文本框的焦点上设置光标长度为 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.

祝你好运

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

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