angularjs 1 Failed to read the 'selectionStart' property from 'HTMLInputElement':

本文介绍了一种在AngularJS应用中实现input[type='number']元素聚焦时选中全部文本的方法,通过自定义一个名为selectOnClick的directive指令,解决了在Ionic环境中无法自动选中文本的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在找angularjs input(type='number')在获取焦点的时候,文本框内容选中效果,参考了:Select text on input focus,我直接复制他的code之后,在ionic中报"Uncaught InvalidStateError: Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection.",

尝试了 

---

this.setSelectionRange(0, 9999)

---

this.selectionStart = 0;
this.selectionEnd = 999;

 

上面两个选中文本,我在webapp中测试的时候,需要每次双击之后才会有效果,我尝试用jQuery的select()方法来达到这个效果,再ionic编译的apk中测试可用之后,觉得这是我目前的解决方法。

directive方法代码

.directive('selectOnClick',  function ($window) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            element.on('click', function () {
                if (!$window.getSelection().toString()) {
                    // Required for mobile Safari
                    $(this).select();
                }
            });
        }
    };
  })

input使用这个directive(指令)像:

<input type="text" value="test" select-on-click />

再android app中,使用type="number"也可以工作。

转载于:https://www.cnblogs.com/fsong/p/5563119.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值