用jquery获取附近的div子元素(Get nearby div child element with jquery)

编程入门 行业动态 更新时间:2024-10-28 12:26:05
用jquery获取附近的div子元素(Get nearby div child element with jquery)

我怎样才能得到附近的div与jquery的子元素? 我想验证()我的输入元素我需要得到class=grand_child_02 ,当我在class=input_01

HTML:

<div class="parent"> <div class="child_01"> <h4 class="grand_child_01_01"> <a href="#" class="link_01">link_01</a> </h4> <div class="grand_child_01_02">Some_text</div> </div> <div class="child_02"> <div class="grand_child_02_01"> <input type="text" class="input_01"/> </div> </div> </div>

谢谢。

How can I get nearby div's child element with jquery ? I'm trying to validate() my input element I need to get class=grand_child_02, when I'm in class=input_01

HTML:

<div class="parent"> <div class="child_01"> <h4 class="grand_child_01_01"> <a href="#" class="link_01">link_01</a> </h4> <div class="grand_child_01_02">Some_text</div> </div> <div class="child_02"> <div class="grand_child_02_01"> <input type="text" class="input_01"/> </div> </div> </div>

Thanks.

最满意答案

澄清我的意见:

我已经把其他的答案都投给了正确的答案,但是只会使用没有过滤器的siblings()而稍微调整一下。

var $target = $(this).closest('.child_02').siblings().find('.grand_child_02');

this是您指定的输入元素。

如果你只是使用兄弟姐妹(),它会与额外的添加兄弟元素元素(这可能在实际情况下发生)。 我认为这种变化更强健一点(并且比使用同胞过滤器慢得多)。

例如在这个例子中:

小提琴: http : //jsfiddle.net/TrueBlueAussie/tw3Yc/1/

For clarification of my comments:

I have upvoted both the other answers as correct, but would suggest a slight tweak by only using siblings() without a filter.

var $target = $(this).closest('.child_02').siblings().find('.grand_child_02');

Where this is the input element you specified.

If you just use siblings(), it will work with additional added sibling div elements (which may be likely to occur in practical situations). I consider this a little more robust to changes (and insignificantly slower than with using the siblings filter).

e.g. in this example:

Fiddle: http://jsfiddle.net/TrueBlueAussie/tw3Yc/1/

更多推荐

本文发布于:2023-07-05 14:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1038504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   div   jquery   element   child

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!