SORU
26 NİSAN 2011, Salı


jQuery - bir button tüm metni seç

Nasıl bir metin alanının içinde tıklatın, tüm içeriği seçilir yani ben yapabilir miyim?

Ve sonunda yine, seçimini kaldırmak için tıklatın.

CEVAP
26 NİSAN 2011, Salı


Durdurmak alma ve rahatsız olduğunda bütün metin alır seçilen her zaman onlar hareket ettirmeye şapka kullanarak kendi fare, bunu gereken kullanma focus olay, click olay. Aşağıdaki iş yapacak ve çalışan basit versiyonu (yani Button select() sadece arama yöntemi focus olay işleyicisi) engelleyen Krom bir sorunu çalışır.

jsFiddle: http://jsfiddle.net/NM62A/

Kod:

<textarea id="foo">Some text</textarea>

<script type="text/javascript">
    var textBox = document.getElementById("foo");
    textBox.onfocus = function() {
        textBox.select();

        // Work around Chrome's little problem
        textBox.onmouseup = function() {
            // Prevent further mouseup intervention
            textBox.onmouseup = null;
            return false;
        };
    };
</script>

jQuery sürüm:

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

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

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Eric Magidson

    Eric Magidso

    4 Ocak 2009
  • Sean Murphy

    Sean Murphy

    4 ŞUBAT 2009
  • YAN TV

    YAN TV

    20 EKİM 2011