繁体   English   中英

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

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

试图弄清楚为什么会发生这种情况-我有一个输入文本字段,当该字段获得焦点时,我希望所有文本都突出显示。 这种情况很快发生,然后所有文本都未被选中。 知道为什么会这样吗? 这是我正在使用的代码:

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

你需要重写输入元件上的鼠标松开事件(如中提到的这篇文章 - !感谢MrSlayer)

例如,请参见此处: http : //jsfiddle.net/f8TdX/

这是WebKit中的问题。 最好的选择是结合使用focusmouseup事件。 以下是对类似问题的另一个答案

$("#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;
    });
});

试一下

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

选择文本框获得焦点时的所有内容(JavaScript或jQuery)

暂无
暂无

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

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