2013-08-02 25 views
0

元素我有以下結構:下面選擇使用jQuery

enter image description here

我嘗試這樣做:

$("#navbar li .arrow").click(function() { 
    alert("worked"); 
    $(this).closest('ul').css("display", "block"); 
}); 

要選擇.arrowul元素。但沒有發生。

有什麼建議嗎?

+0

你試過.next()嗎? – chris

回答

1

.closest()方法獲取的祖先元素,你的情況ul.arrow元素

您需要使用.next()這裏

$(this).next().css("display", "block"); 
0

你可以使用.next()如果ul是下一個兄弟總是在.arrow

$(this).next() 
0

closest()用於選擇最近的匹配父元素。要選擇一個兄弟使用next()

$("#navbar li .arrow").click(function() { 
    $(this).next('ul').css("display", "block"); 
}); 
0

closest()選擇最接近的父對象,則需要使用next()

0

使用.next()這裏:

$(this).next().css("display", "block"); 
0

試試這個,

$("#navbar li a.arrow").click(function() { 
    $(this).next('ul').css("display", "block"); 
});