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


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

查看所有标签

猜你喜欢:

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

Bad Blood

Bad Blood

John Carreyrou / Knopf / 2018-5-21 / USD 27.95

The full inside story of the breathtaking rise and shocking collapse of Theranos, the multibillion-dollar biotech startup, by the prize-winning journalist who first broke the story and pursued it to t......一起来看看 《Bad Blood》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具