将元素的子元素添加到 jQuery 集合而不离开链
是否有可能在不离开链的情况下将 jQuery 集合中元素的子元素添加到集合中?
即,与离开它看起来像这样:
var set = $('.some-elements');
set.add('*', set);
// or immediate ones only
set.add(set.children());
我正在寻找一种方法来执行这些操作而不创建临时set
变量:(
$('.some-elements').jqMagic("*");
当然,人们可以写一个我只是想确定一下,jQuery 核心中没有这个功能。)
Is there a possibility to add the children of elements in a jQuery set to the set without leaving the chain?
I.e., with leaving it would look like this:
var set = $('.some-elements');
set.add('*', set);
// or immediate ones only
set.add(set.children());
I'm looking for a way to do any of these without creating the interim set
variable:
$('.some-elements').jqMagic("*");
(Of course one could write a "jqMagic" plugin for this. I just want to be sure, there isn't this functionality in jQuery core.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 AndSelf 会让您做您所要求的事情。
用示例更新:
给定以下 html:
此代码仅为子级(在本例中为锚标记)提供红色边框:
而此代码(使用 andSelf())在元素列表中包含父级 div因此选择父 div 和子 div - 从而根据您的要求给出合并集:
I believe AndSelf will let you do what you're asking.
update with example:
Given the following html:
This code gives just the children (anchor tags in this case) a red border:
while this code (using andSelf()) includes the parent divs in the element list and therefore selects both the parent divs and the child divs - thus giving a merged set as you requested: