我怎样才能得到附近的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/
更多推荐
发布评论