简体   繁体   English

突出显示输入文本不起作用

[英]Highlight input text not working

Hello I would like the text inside an input element to highlight upon initial click. 您好,我希望输入元素内的文本在首次单击时突出显示。 However my function does not seem to be working. 但是,我的功能似乎无法正常工作。 I have researched the issue and seen that there are some issues with jquery 1.7 and below. 我研究了这个问题,发现jQuery 1.7及以下版本存在一些问题。 I have adjusted it to account for this any it still does not work. 我已对其进行调整以解决此问题,但仍然无法正常工作。

Any help would be great. 任何帮助都会很棒。 Thanks! 谢谢!

HTML 的HTML

 <input type="text" value="hello"/>

JS JS

  $scope.highlightText = function() {
    $("input[type='text']").on("click", function() {
      $(this).select();
    });

https://plnkr.co/edit/b7TYAFQNkhjE6lpRSWTR?p=preview https://plnkr.co/edit/b7TYAFQNkhjE6lpRSWTR?p=preview

You need to actually call your method at the end of controller, otherwise the event is not bound. 您实际上需要在控制器的末尾调用您的方法,否则该事件未绑定。

https://plnkr.co/edit/a0BlekB8qTGOmWS8asIx?p=preview https://plnkr.co/edit/a0BlekB8qTGOmWS8asIx?p=preview

... other code ... ...其他代码...

        $scope.highlightText = function () {
                    $("input[type='text']").on("click", function () {
                        $(this).select();
                        var test = $(this).parent();
                        console.log(test);
                    });

        $("textarea").on("click", function () {
                        $(this).select();
            });
        };
    $scope.highlightText();
  };

要在输入中选择文本,您只需在onclick调用this.select() ,如下所示

 <input type="text" onclick="this.select()" value="hello"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM