javascript – 类型电子邮件不支持selectionrange

栏目: JavaScript · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/26658474/type-email-doesnt-support-selectionrange

当我在文本框的焦点上时,我正试图将光标设置到开头的位置.这就是我所拥有的:

$("ID").focus(function () {
    var input = this;
    setTimeout(function() {
        input.setSelectionRange(0, 0);
    }, 0);
});

但是每次我尝试加载脚本时都会收到此错误:

Uncaught InvalidStateError: Failed to execute setSelectionRange on HTMLInputElement’: The input elements type (’email’) does not support selection. 

猜猜我不能在电子邮件上使用setSelectionRange.那么,关于如何在输入文本框中设置光标位置的任何其他解决方案(不更改类型电子邮件)?

我知道这是一个老问题,但我会回答它以供将来参考.

使用setSelectionRange输入的电子邮件目前正在与所有主流浏览器一起使用.

这是一个例子:

jQuery('.email').focus(function() {
  jQuery(this)[0].setSelectionRange(2, 5);
})
.email
{
 padding: 15px;
  width: 350px
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="email" name="email" class='email' value='testin_email_selection'>

jsFiddle: https://jsfiddle.net/eLz6ukmm/

在FF33和Edge中测试过.

翻译自:https://stackoverflow.com/questions/26658474/type-email-doesnt-support-selectionrange


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Once Upon an Algorithm

Once Upon an Algorithm

Martin Erwig / MIT Press / 2017-9-8 / GBP 22.95

How Hansel and Gretel, Sherlock Holmes, the movie Groundhog Day, Harry Potter, and other familiar stories illustrate the concepts of computing. Picture a computer scientist, staring at a screen and......一起来看看 《Once Upon an Algorithm》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试