简体   繁体   中英

Combine functions with selectors in jQuery

I need to select the parent element of the current element, then going to the next one and selecting the anchor tag inside that element. So for example, my DOM looks like this:

-td
  - span  (my element)
- td
- a

So what am doing is:

$(this).parent().next().("a") //doesn't work

How can I combine the next() function with a selector like a or select something by its class or id?

I couldn't find anything on jQuery documentation for that.

你可以用这个

$(this).parent().next().find("a") //it work

With your current set-up, using this works:

$(this).parent().next("a");

By the way, your markup is wrong . You cannot have a <a> as a sibling of <td> , which means, either the <td> is not inside <tr> or <a> is inside <tr> , which is wrong . So I assume your markup is similar to this:

<td>
  <span></span>
</td>
<td>
  <!-- -->
</td>
<a></a>

So, if you are putting it like this ( valid markup ):

<td>
  <span></span>
</td>
<td>
  <!-- -->
</td>
<td>
  <a></a>
</td>

You need to use:

$(this).parent().next().children("a");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM