简体   繁体   English

在Firefox中单击鼠标时无法选择所有输入元素文本

[英]Having trouble selecting all input element text on mouse click in Firefox

I've had great success with the following in the latest IE/Chrome: 在以下最新的IE / Chrome浏览器中,我取得了巨大的成功:

<input value="Test"/>
<script>
require(["dojo/on", "dojo/query", "dojo/domReady!"], function(on, query){
    var blah = query("input")[0];
    on(blah, "focus", function(e){
   this.setSelectionRange(0,9999);
});
});
</script>

The purpose is to select all the text in an input element when they click it (just like how it selects all text when tabbing in by default), so when they start typing it erases what was in there and starts afresh. 目的是在单击输入元素时选择输入文本中的所有文本(就像默认情况下在按Tab键时选择所有文本一样),因此当他们开始键入内容时,它将擦除其中的内容并重新开始。

However, in Firefox (SeaMonkey too) even with the above code something SOMETIMES unselects the text on mouseup and reverts to the default case of putting the caret where the user clicks and selecting nothing. 但是,在Firefox(也包括SeaMonkey)中,即使使用上面的代码,有时SOMETIMES也会取消选择mouseup上的文本,并恢复为默认的情况,即将插入符号放置在用户单击的位置,而不进行任何选择。 You will always see that the text is all selected at least for a split second. 您将始终看到所有文本至少在一秒钟内被选中。

What can I do? 我能做什么? Any suggestions? 有什么建议么?

See fiddle here: 在这里看到小提琴:

http://jsfiddle.net/avxra2q2/11/ http://jsfiddle.net/avxra2q2/11/

I really can't figure out why this is happening in firefox and I also tested what are you doing in jquery and also the recommended way by MDN(Mozilla Developer network), and because of that I am going to assume it is a bug. 我真的不知道为什么在firefox中会发生这种情况,我还测试了您在jquery中的操作以及MDN(Mozilla开发人员网络)推荐的方式,因此,我将假定它是一个错误。

but I got you a temp solution that will work, try this in JSfiddle I got to this answer because my experience with browsers especially when you are playing with focus and cursors things doesn't go your way so the browser need some time. 但是我为您提供了一个可以使用的临时解决方案,请在JSfiddle中尝试一下 ,我得到了这个答案,因为我在浏览器上的体验,特别是当您使用焦点和游标进行游戏时,事情并没有如您所愿,因此浏览器需要一些时间。

you can also look at the code below 您还可以查看下面的代码

require(["dojo/on", "dojo/query","dojo/has", "dojo/sniff", "dojo/domReady!"], function(on, query,has,sniff){
    var blah = query("input")[0];
    on(blah, "focus", function(e){

        if(has("ff")){ // only firefox 
       var myVar = setTimeout(function(){
                      clearTimeout(myVar);
                     blah.focus();
                blah.setSelectionRange(0,9999);      
                   },150);
        }else{
              this.setSelectionRange(0,9999);
        }

});
});

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

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