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

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

内容简介:翻译自: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 element’s 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


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

查看所有标签

猜你喜欢:

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

Haskell

Haskell

Simon Thompson / Addison-Wesley / 1999-3-16 / GBP 40.99

The second edition of Haskell: The Craft of Functional Programming is essential reading for beginners to functional programming and newcomers to the Haskell programming language. The emphasis is on th......一起来看看 《Haskell》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换