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


当前回答

这对我来说很管用(因为它不是在回答中,而是在评论中)

 $("#textBox").focus().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()}
/>

这不仅仅是Chrome/Safari的问题,我在Firefox 18.0.1中也遇到了类似的问题。有趣的是,这不会发生在MSIE!这里的问题是第一个鼠标点击事件强制取消选择输入内容,所以忽略第一个事件。

$(':text').focus(function(){
    $(this).one('mouseup', function(event){
        event.preventDefault();
    }).select();
});

timeOut方法会导致一种奇怪的行为,并且阻塞每个鼠标升起事件,您无法删除再次单击输入元素的选择。

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

<输入类型=“文本”

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

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