使用 jquery 为有孩子的父母添加类
我将如何仅向有子项的 li
父级添加一个类?
<ul>
<li><a href="#">parent</a></li>
<li><a href="#">parent</a>
<ul>
<li>child</li>
<li>child</li>
</ul>
</ul>
How would I go about adding a class only to the li
parent that has children?
<ul>
<li><a href="#">parent</a></li>
<li><a href="#">parent</a>
<ul>
<li>child</li>
<li>child</li>
</ul>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者
如果您这样做,第一个版本可能会稍微快一点:
...虽然没有测试,但很难说。
所有这些都使用完全有效的 CSS 选择器。
or
The first version may be marginally faster if you do:
...hard to say though without testing.
All of these use fully valid CSS selectors.
我喜欢 jQuery 使某些事情变得如此简单。
这是上述代码的 jsfiddle: http://jsfiddle.net/p9M73/
这是 <代码>:has(): http://api.jquery.com/has-selector/
I love how simple jQuery makes some things.
Here is jsfiddle of the above code: http://jsfiddle.net/p9M73/
Here is the documentation for
:has()
: http://api.jquery.com/has-selector/