2012-07-21 22 views

cevap

19

Referans it: bulanıklık olay gerçekleşir gerçekleşmez

document.activeElement

Maalesef yeni eleman odaklı değildir, bu nedenle bu vücudu bildirir. Yani, bayraklar ve odaklama olayı ile hacklemeniz ya da setTimeout'u kullanmanız gerekecek.

$("input").blur(function() { 
    setTimeout(function() { 
     console.log(document.activeElement); 
    }, 1); 
});​ 

İyi çalışıyor.


setTimeout olmadan, bu kullanabilirsiniz: Neden focusOut olayı kullanmayan

http://jsfiddle.net/RKtdm/

(function() { 
    var blurred = false, 
     testIs = $([document.body, document, document.documentElement]); 
    //Don't customize this, especially "focusIN" should NOT be changed to "focus" 
    $(document).on("focusin", function() { 

     if (blurred) { 
      var elem = document.activeElement; 

      blurred = false; 

      if (!$(elem).is(testIs)) { 
       doSomethingWith(elem); //If we reached here, then we have what you need. 
      } 

     } 

    }); 
    //This is customizable to an extent, set your selectors up here and set blurred = true in the function 
    $("input").blur(function() { 
     blurred = true; 
    }); 

})();​ 

//Your custom handler 
function doSomethingWith(elem) { 
    console.log(elem); 
} 
+0

Bundan korktum. Eğer setTimeout çözümü yoksa, bu cevabı birkaç gün içinde kabul edeceğim. – fadedbee

+0

@chrisdew Odak olayı ile bir şeyler yapabilirim, ancak bunun basit bir zaman aşımından daha karmaşık olacağına inanıyorum. – Esailija

+0

@chrisdew bu sizin için çalışıyor mu? http://jsfiddle.net/RKtdm/ – Esailija

İlgili konular