2012-10-23 25 views
2

我想用jquery查找下一個div。這裏是代碼:jquery查找下一個(指定)元素不工作

$('.menu-item').hover(function() { 
    $(this).stop(true, true).addClass("menu-item-hover", 300); 
    $(this).next('div').stop(true, true).fadeIn(300); 
}, function() { 
    $(this).stop(true, true).removeClass("menu-item-hover", 300); 
    $(this).next('div').stop(true, true).fadeOut(300); 
}); 


<a href="media.html"><div class="menu-item"><h1>Media</h1></div></a> 
<div id="tooltip-media">Sights and sounds from Cambodia</div> 

<a href="about.html"><div class="menu-item"><h1>About</h1></div></a> 
<div id="tooltip-about">Who in the world is the Phillips Family?</div> 

簡單。divs隱藏在css中。我希望代碼中的下一個顯示在menu-item的懸停上。我檢查了其他問題,他們都沒有爲我的案件工作。我正在使用Jquery和Jquery UI。謝謝你的幫助。

+0

能否請您爲您的例子小提琴。 –

+1

.next()適用於兄弟姐妹。您選擇'.menu-item',但這些嵌套在'a'元素中,這意味着他們沒有兄弟姐妹。 –

+0

感謝您的幫助。得到它的工作。沒有實現'next()'的功能。 – preahkumpii

回答

1

試試這個

$(this).parent().next('div').stop(true, true).fadeIn(300); 
在你的榜樣

,你正在努力尋找下一個div但不sibiling菜單項。 只有它沒有工作。遍歷父母,並找到下一個div它會工作。

parent()next()

+0

@jAndy:老兄我跟着作者的例子。我寫了'查詢'基於它的例子它將工作。 – YogeshWaran

+0

你是對的,我很抱歉。沒有意識到這個標記。 – jAndy

+0

@jAndy:沒問題。感謝您的支持:)。 – YogeshWaran

1

首先,.next()只有抓住眼前下一個兄弟,它不會通過所有sibilings搜索。爲此,您需要調用.nextAll('div')。但即使這個函數也不會幫助你,因爲你正在尋找的<div>節點不包含在相同的父元素中。

你應該去像

$(this).parent().next('div[id^=tooltip]').stop(true, true).fadeOut(300); 

參考:.next().nextAll()