我需要 jQuery 代码,我可以使用它来查找至少一个 div 的直接父级,而这些 divul
又具有 3 个以上的元素。当div
找到这样的一个,它应该添加类名target
给它。
例如,如果我有以下 HTML 代码:
JSFiddle 这里:http: //jsfiddle.net/AvkY5/
<div class="main">
<div class="main_child_1">
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
</div>
<div class="main_child_2">
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
</div>
<div class="main_child_3">
<p>Lorem Ipsum</p>
</div>
<div class="main_child_4">
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
<div class="main_child_4_child_1">
<p>Lorem Ipsum</p>
</div>
<div class="main_child_4_child_2">
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
</div>
</div>
<div class="main_child_5">
<p>Lorem Ipsum</p>
<div class="main_child_5_child_1">
<p>Lorem Ipsum</p>
</div>
<div class="main_child_5_child_2">
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
</div>
</div>
<div class="main_child_6">
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
<div class="main_child_6_child_1">
<p>Lorem Ipsum</p>
</div>
<div class="main_child_6_child_2">
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
</div>
<div class="main_child_6_child_3">
<p>Lorem Ipsum</p>
</div>
</div>
</div>
然后在此代码中,应将以下 divtarget
添加到其类名中:
- main_child_2
- main_child_4
- main_child_5_child_2
- main_child_6_child_2
什么 jQuery 代码可以实现这一点?