什么是香草JS或jQuery解决方案,将选择一个文本框的所有内容时,文本框接收焦点?


当前回答

$('input').focus(function () {
    var self = $(this);
    setTimeout(function () {
        self.select();
    }, 1);        
});

编辑:根据@DavidG的请求,我不能提供详细信息,因为我不确定为什么这样工作,但我相信这与向上或向下传播的焦点事件或任何它所做的事情有关,输入元素获得它所接收的焦点通知。设置timeout会给元素一点时间来意识到它已经这样做了。

其他回答

我知道内联代码是不好的风格,但我不想把它放在.js文件中。 不用jQuery也能工作!

<input type="text" value="blah blah" onfocus="this.select(); this.selAll=1;" onmouseup="if(this.selAll==0) return true; this.selAll=0; return false;"></input>

<输入类型=“文本”

我知道这里已经有很多答案了——但这一个到目前为止还没有找到;一个解决方案,也与ajax生成的内容:

$(function (){
    $(document).on("focus", "input:text", function() { 
        $(this).select(); 
    });
});

HTML:

var textFiled = document.getElementById("text-filed"); 文本文件。addEventListener("focus", function() {this.select();}); 输入你的文本:<input type=" Text " id=" Text -filed" value="test with filed Text ">

使用JQuery:

$("#text-filed").focus(function() { $(this).select(); } );

使用React JS:

在各自的组成部分-

<input
  type="text"
  value="test"
  onFocus={e => e.target.select()}
/>

这个会有用的,试试这个

<input id="textField1" onfocus="this.select()" onmouseup="return false" /> 

可以在Safari/IE 9和Chrome上运行,但我没有机会在Firefox上测试。