2012-07-10 42 views
0

傢伙:)麻煩與針對一個元素的jQuery

$('#loginbox').blur(function(){ 
    var focusedInputs = null; 
    $('#loginbox table tbody tr td input').focus(function(){ 
     focusedInputs = this; 
    }); 
    alert(focusedInputs); 
    if (focusedInputs == null) { 
     $('#loginbox').stop().fadeOut('fast',function(){ 
      $('#loginlink').prop('href','#login'); 
      window.location.hash = '#'; 
     }); 
     $('#loginlink').removeClass('selected'); 
     $('.tooltip').css('display','none'); 
     Cufon.refresh(); 
    } 
}); 

這alertrs我 'NUL',但如果我寫警報的對焦功能(focusedInputs):

$('#loginbox table tbody tr td input').focus(function(){ 
     focusedInputs = this; 
     alert(focusedInputs); 
    }); 

它告誡元素...我不知道問題在哪裏......謝謝!

+0

爲什麼不試試'$(this)'而不是'this'? – 2012-07-10 21:28:25

+0

如果該字段重點關注,警報是否只有值? – j08691 2012-07-10 21:32:12

回答

0

試試這個,你真的不應該在blur事件中綁定焦點事件,除非你打算解除綁定。

var focusedInputs = null; 
$('#loginbox table tbody tr td input').focus(function(){ 
    focusedInputs = this; 
}); // do you also need a blur event here to clear focusedInputs? 


$('#loginbox').blur(function(){ 
    alert(focusedInputs);  
    if (focusedInputs == null) { 
     $('#loginbox').stop().fadeOut('fast',function(){ 
      $('#loginlink').prop('href','#login'); 
      window.location.hash = '#'; 
     }); 
     $('#loginlink').removeClass('selected'); 
     $('.tooltip').css('display','none'); 
     Cufon.refresh(); 
    } 
}); 
+0

這很好,但焦點事件將不會被綁定,直到你模糊登錄框之後...每次之後導致事件被綁定n次,其中n是你有blured登錄框的次數。 – 2012-07-10 21:40:50

+0

我需要檢查#loginbox中的任何輸入是否僅在#loginbox發生模糊事件時纔會關注。 我試過了。空值... – 2012-07-10 21:42:46