Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery选择下面的元素_Jquery - Fatal编程技术网

使用jQuery选择下面的元素

使用jQuery选择下面的元素,jquery,Jquery,我的结构如下: 我试过这个: $( "#navbar li .arrow" ).click(function() { alert("worked"); $(this).closest('ul').css("display", "block"); }); 选择箭头下的ul元素。但什么也没发生 有什么建议吗?该方法获取祖先元素,在您的例子中,ul是.arrow元素的下一个同级元素 你需要在这里使用 如果ul始终位于下,则可以使用.next() $(this).next() closes

我的结构如下:

我试过这个:

$( "#navbar li .arrow" ).click(function() {
  alert("worked");
  $(this).closest('ul').css("display", "block");
});
选择
箭头下的
ul
元素。但什么也没发生

有什么建议吗?

该方法获取祖先元素,在您的例子中,
ul
.arrow
元素的下一个同级元素

你需要在这里使用

如果
ul
始终位于
下,则可以使用
.next()

$(this).next()
closest()
用于选择最近的匹配父元素。要选择同级,请使用
next()

nextest()
选择最近的父对象,您需要使用
next()
使用
。next()
此处:

$(this).next().css("display", "block");
试试这个

$( "#navbar li a.arrow" ).click(function() {
    $(this).next('ul').css("display", "block");
});
你试过了吗。下一步()?
$(this).next().css("display", "block");
$( "#navbar li a.arrow" ).click(function() {
    $(this).next('ul').css("display", "block");
});