简体   繁体   English

jQuery-字段选择所有文本,然后取消选中焦点

[英]jquery - field selects all text then unselects it on focus

trying to figure out why this is happening - I have an input text field and I want all the text to be highlighted when the field receives focus. 试图弄清楚为什么会发生这种情况-我有一个输入文本字段,当该字段获得焦点时,我希望所有文本都突出显示。 This happens, very quickly, and then all of the text is unselected. 这种情况很快发生,然后所有文本都未被选中。 Any idea why this would occur? 知道为什么会这样吗? Here's the code I'm using: 这是我正在使用的代码:

$("#permalink").focus(function(){
    this.select();
});

You need to override the mouseup event on the input element (as mentioned in this post - thanks MrSlayer!) 你需要重写输入元件上的鼠标松开事件(如中提到的这篇文章 - !感谢MrSlayer)

See here for example: http://jsfiddle.net/f8TdX/ 例如,请参见此处: http : //jsfiddle.net/f8TdX/

This is an issue in WebKit. 这是WebKit中的问题。 The best option is to use a combination of the focus and mouseup events. 最好的选择是结合使用focusmouseup事件。 The following comes from another answer to a similar question. 以下是对类似问题的另一个答案

$("#permalink").focus(function() {
    var $this = $(this);

    $this.select();

    window.setTimeout(function() {
        $this.select();
    }, 1);

    // Work around WebKit's little problem
    $this.mouseup(function() {
        // Prevent further mouseup intervention
        $this.unbind("mouseup");
        return false;
    });
});

Give this a shot 试一下

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

Select all contents of textbox when it receives focus (JavaScript or jQuery) 选择文本框获得焦点时的所有内容(JavaScript或jQuery)

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

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