2012-06-15 45 views
2

您好我現在用鼠標離開()下拉導航菜單中,這樣當用戶離開子菜單下拉子菜單中消失,但它似乎忽略它,菜單依然存在。有任何想法嗎?下面是該網站並代碼:jQuery的鼠標離開()被忽略

http://www.maiagifts.co.uk/about-us/info_1.html

$(document).ready(function() { 

    $('#newcats li').addClass('parentitem'); 
    $('.newsubs li').removeClass('parentitem'); 
    $('.newsubs').hide();      

    $('.parentitem').hover(    
    function(){ 
    $(this).children('.newsubs').show(); 
    $(this).siblings('.parentitem').children('.newsubs').hide(); 

}); 


    //problem is here// 
    $('.newsubs').mouseleave(
    function(){ 
    $(this).hide(); 
    }); 
    //problem is here// 


    }); 

回答

4

.on

$('.newsubs').on('mouseleave', function(){ 
    $(this).hide(); 
}); 

嘗試,如果你正在使用jQuery的版本低於1.7.1,然後使用.live()

+0

將不適用於OP。嘗試$(「body」)。在問題 –

+0

$(selector).live的鏈接的開發控制檯上爲我工作 – Francisco

0

你試過:

$('.newsubs').mouseleave(
    function(){ 
    $('.newsubs').hide(); 
    }); 

會實現你想要的嗎?

0

如果你看看你正在鏈接到的網站的開發控制檯,你會看到一堆錯誤消息之一(重複)爲"Uncaught TypeError: Object #<Object> has no method 'mouseleave'",所以上面的代碼附加處理程序永遠不會執行。

.on也沒有定義,我猜他們都是你有第一個錯誤消息的結果,這是在jQuery中第91行的 Uncaught TypeError: Cannot read property 'guid' of undefined

換句話說,jQuery腳本沒有執行全長,而mouseleave和其他方法如on從未定義過。

0

我將hover()方法更改爲.mouseover()並開始工作。我想知道是否因爲我使用hover()函數僅用於它的鼠標懸停部分,jquery已經決定它有一個空的mouseleave,所以忽略了第二個。只是一個猜測。