简体   繁体   English

结合使用jQuery中的选择器功能

[英]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: 因此,例如,我的DOM如下所示:

-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? 如何将next()函数与选择器(如a)结合使用,或按其类或id选择某项?

I couldn't find anything on jQuery documentation for that. 我在jQuery文档上找不到任何东西。

你可以用这个

$(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 . 您不能将<a>作为<td>的同级,这意味着<td>不在<tr>或者<a><tr> ,这是错误的 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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