我有一些标记,比如
<div class="a"><img></div>
和
<div class="b"><img></div>
我需要用div动态包装img,并使用$(":not('.a, .b) > img")
来选择它们。
不幸的是,我也有一些标记,比如
<div class="c"><div><div><img></div></div></div>
因此>子组合子不起作用。我猜$(":not('.a, .b, .c) img")
对所有人都有效,但事实并非如此。
我想念什么?如何选择属于多个类但处于不同子级的所有img标记?
感谢的帮助
我错过了什么?
:not('.a, .b, .c)
匹配没有任何类的内部div
,这些类也是img
的祖先。
您可能可以使用:not
和jQuery的:has
扩展的某种组合,但我认为我可能会手动执行,类似于以下内容:
// Using jQuery
$("img").each((_, el) => {
const $el = $(el);
if (!$el.closest(".a, .b, .c").length) {
// ...wrap the element...
}
});
或
// Using the DOM directly
for (const img of document.querySelectorAll("img")) {
if (!img.closest(".a, .b, .c")) {
// ...wrap `img`...
}
}
因为DOM现在具有CCD_ 8。