选择所选孩子的父母
首先,如果这是一个多余的问题,请原谅我。我想您会同意,合理地专门搜索这是一个困难的案例。
我有看起来像
<li class='signature'>
<span class='label'>Signature:</span>
<div class='folding'>
<span class='data'></span>
</div>
</li>
<li class='issuer'>
<span class='label'>Issuer:</span>
<span class='data'></span>
</li>
等的html。
我想使用jquery仅对包含“folding”类的div的任何li执行.addClass,但不对其他li执行。我确信我可以使用巧妙设计的 .each 语句来做到这一点,但我直觉地认为必须有一种方法可以仅使用 jquery 选择器来做到这一点。我已经做到了,
$('li > .moreclick').addClass("arrowable");
但这当然将类添加到跨度中,而不是添加到里中。
我是否被 .each 困住了? (如果是这样,任何关于最佳方法的提示都会很好,但不可否认,这超出了问题的范围)。
谢谢!
First off, forgive me if this is a redundant question. I think you'll agree that it's a difficult case to reasonably specifically search for.
I have html that looks like
<li class='signature'>
<span class='label'>Signature:</span>
<div class='folding'>
<span class='data'></span>
</div>
</li>
<li class='issuer'>
<span class='label'>Issuer:</span>
<span class='data'></span>
</li>
etc etc.
I want to use jquery to do an .addClass only to any li that contains a div of the class "folding", but not to the other lis. I'm sure that I can do this using a cunningly-crafted .each statement, but I kind of feel intuitively that there must be a way to do this using just jquery selectors. I have gotten as far as
$('li > .moreclick').addClass("arrowable");
but that of course adds the class to the span, and not to the li.
Am I stuck with an .each? (and if so, any hints on the best way to do that would be nice, but admittedly outside the scope of the question).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果
li
始终是绝对父级:否则,这将继续搜索层次结构,直到找到
li
:[编辑]
迪奥斯拉斯卡的解决方案要好得多。不过,将我的保留在这里以供参考。
If the
li
is always going to be the absolute parent:Otherwise, this will keep searching up the hierarchy until it finds an
li
:[edit]
dioslaska's solution is much nicer. Keeping mine here for reference though.
尝试以下
jQuery 文档
Try the following
jQuery documentation
例如:
//编辑太慢..
for example:
//edit too slow..
试试这个。
try this.
如果您只想检查 li 的直接子代:
If you want to check only the direct children of li:
请尝试这样做:
这将准确选择
li
元素,该元素是具有'folding'
类的div
的父元素。Please try this:
This will selects exactly the
li
element which is the parent ofdivs
that have class'folding'
.