Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

javascript - 电子邮件类型不支持选择范围(Type Email doesn't support selectionrange)

I'm trying to set my cursor to the position of the beginning when I'm on focus of a text box.(当我将焦点放在文本框上时,我试图将光标设置在开始位置。)

This is what I have:(这就是我所拥有的:)
$("ID").focus(function () {
    var input = this;
    setTimeout(function() {
        input.setSelectionRange(0, 0);
    }, 0);
});

But I get this error every time I try to load the script:(但是,每次尝试加载脚本时都会出现此错误:)

Uncaught InvalidStateError: Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('email') does not support selection.(未捕获的InvalidStateError:无法在“ HTMLInputElement”上执行“ setSelectionRange”:输入元素的类型(“电子邮件”)不支持选择。)

Guess I can't use setSelectionRange on emails.(猜猜我不能在电子邮件上使用setSelectionRange 。)

So, any other solutions on how to set my cursor position in the input text box (without changing the type email)?(那么,关于如何在输入文本框中设置光标位置的其他解决方案(不更改电子邮件类型)呢?)   ask by Norman In translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is true, it seems that there is a bug when using the setSelectionRange function for an email input.(的确,使用setSelectionRange函数进行电子邮件输入时似乎存在一个错误。)

So what can be done is to modify the type of the input so that setSelectionRange works and not error.(因此,可以做的是修改输入的类型,以便setSelectionRange起作用而不出错。)
$('#ID').focus(function () {
        var input = this;
        setTimeout(function () {
            $(input).attr('type', 'text');
            input.setSelectionRange(0, 0);
            $(input).attr('type', 'email');
        }, 100);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...