2013-08-06 59 views
0

我有一個2組3個輸入的小表格。jQuery - 焦點/模糊顯示/隱藏輸入組

每組只有一個輸入可見。

no input focused

如果我焦點的輸入,所有在同一組中被示出。

Group1 input focused

如果沒有該組的輸入被聚焦時,僅第一個應該出現。

Group2 input focused

$('input').focus(function() { 
    $(this).closest('div').addClass('focused'); 
}).blur(function() { 
    $(this).closest('div').removeClass('focused'); 
}) 

這個工作在Chrome,但不能在Firefox(請my fiddle)。
在獲得焦點之前,右側輸入都被隱藏。
似乎事件泡泡順序在兩個瀏覽器上都不同。

有人可以幫我做這項工作的所有瀏覽器?

+0

我會認爲它可能是由於你不刪除'當您添加了'.focused'類.hide'類。試試看看是否有效。 – irot

+0

情況並非如此,'.focused .hide'優先於'.hide' –

回答

2

您可以添加一個小的延遲來防止這種情況發生。

$('input').focus(function() { 
    var $parent = $(this).closest('div'); 
    var timeoutId = $parent.data('tid'); 

    if (timeoutId) { 
     // Aborting the blur 
     clearTimeout(timeoutId); 
    } 

    $parent.addClass('focused'); 
}).blur(function() { 
    var $parent = $(this).closest('div'); 

    var tid = setTimeout(function(){ 
     $parent.removeClass('focused'); 
    }, 1); 

    $parent.data('tid', tid); 
}) 

http://jsfiddle.net/b439u/5/

+0

好的和簡單的想法謝謝 –