2011-05-10 103 views
1

我目前有代碼顯示/隱藏div時,另一個div是懸停或關閉。獲取div顯示/隱藏懸停在jQuery與ajax內容

$(document).ready(function() { 
    $('.eventViewWrapper').hover(function(){ 
     var eventId = $(this).attr('rel'); 
     $("#eventActions_" + eventId).show(); 
    }, 
    function(){ 
     var eventId = $(this).attr('rel'); 
     $("#eventActions_" + eventId).hide(); 
    }); 
}); 

然而,當我通過Ajax後獲取更多的數據,的div不再顯示當我將鼠標懸停: 此代碼爲數據的INTIAL批它是適用於工作正常。 我明白爲什麼會發生這種情況,因爲它發生在以前的ajax功能上,並需要實施.live()方法。所以我做了以下更改:

$(document).ready(function() { 
    $('.eventViewWrapper').live('hover', function() { 
     var eventId = $(this).attr('rel'); 
     $("#eventActions_" + eventId).show(); 
    }, 
    function(){ 
     var eventId = $(this).attr('rel'); 
     $("#eventActions_" + eventId).hide(); 
    }); 
}); 

現在,這部分工作,因爲div在我懸停時出現。但是,當我把注意力放在div上時,它並沒有隱藏起來。我想代碼的第二部分需要將.live()方法鏈接到它,我只是不知道如何?

感謝

回答

1

您可以簡單地使用mouseentermouseleave的事件,因爲這是.hover()內部使用:

$('.eventViewWrapper').live('mouseenter', function() { 
    var eventId = $(this).attr('rel'); 
    $("#eventActions_" + eventId).show(); 
}).live('mouseleave', function() { 
    var eventId = $(this).attr('rel'); 
    $("#eventActions_" + eventId).hide(); 
}); 
+0

謝謝!這就是訣竅! – Cheeky 2011-05-10 13:47:31

相關問題