jQuery wrapAll子类,如果它们还没有具有特定类的父类
我使用以下命令将所有项目包装为“child”类和“parent”类的div类:jQuery wrapAll子类,如果它们还没有具有特定类的父类,jquery,Jquery,我使用以下命令将所有项目包装为“child”类和“parent”类的div类: $(".child").wrapAll('<div class="parent" />'); $(“.child”).wrapAll(“”); 问题是函数有时会重新运行,我不希望创建多个div.parents。如何向代码中添加条件,以便仅在div.childs“尚未具有类为“parent”的父级时添加div.parent?谢谢您可以这样使用: if (! $(".child").parent().ha
$(".child").wrapAll('<div class="parent" />');
$(“.child”).wrapAll(“”);
问题是函数有时会重新运行,我不希望创建多个div.parents。如何向代码中添加条件,以便仅在div.childs“尚未具有类为“parent”的父级时添加div.parent?谢谢您可以这样使用:
if (! $(".child").parent().hasClass('parent')){
$(".child").wrapAll('<div class="parent" />');
}
不知道是不是很快:
$(":not(.parent) > .child").wrapAll('<div class="parent" />');
$(“:not(.parent)>.child”).wrapAll(“”);
未测试!!但可能有效(您可以在一行中完成) $(“.child”).parents('p:not(.parent')).find(“.child”).wrapAll('')
$(":not(.parent) > .child").wrapAll('<div class="parent" />');