簡體   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