开发者

jQuery - next(element) not working

开发者 https://www.devze.com 2023-01-24 16:50 出处:网络
I have this structure: <ul> <li> <a> link .. </a> <span>开发者_C百科; text </span>

I have this structure:

<ul>
  <li> 
    <a> link .. </a>
    <span>开发者_C百科; text </span>
    <ul>
      ...
    </ul>
  </li>
  ...

I'm adding a click event on both the <span> and <a> elements and I'm trying to select the nested <ul> using $(this).next("ul");

It works for the span, but not for the link. What am I doing wrong here?


A click handler on the anchor will not see the UL if you're using next to grab it, since next will only select the very next sibling. You can try:

$("a").nextAll("ul").hide();

or:

$("a").parent().find("ul").hide();

or:

$("a").siblings("ul").hide();
0

精彩评论

暂无评论...
验证码 换一张
取 消