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


当前回答

<输入类型=“文本”

其他回答

我的解决方案是:

var mouseUp;
$(document).ready(function() {
    $(inputSelector).focus(function() {
        this.select();
    }) 
    .mousedown(function () {
        if ($(this).is(":focus")) {
          mouseUp = true;
        }
        else {
          mouseUp = false;
        }
     })
     .mouseup(function () {
        return mouseUp;
     });
});

因此,鼠标通常会工作,但不会在通过输入获得焦点后取消选择

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

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

 $("#textBox").focus().select();

我在某个地方播种了这个,工作得很完美!

            $('input').on('focus', function (e) {
                $(this)
                $(element).one('mouseup', function () {
                        $(this).select();
                        return false;
                    })                        .select();
            });

我知道内联代码是不好的风格,但我不想把它放在.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>