繁体   English   中英

我将如何做一个相当于使用伪类查询多个选择器的 vanilla JS?

[英]How would I do a vanilla JS equivalent of querying multiple selectors with pseudoclasses?

所以我有这个功能:

function populate(selector, snippet, location) {
    var OGsnippet = $('snippet#' + snippet).children().clone().appendTo(selector);
}

它被这样调用: populate('section#main articles article:not(".ad")', 'socmed-articles'); populate('section div.articles.news article:not(".ad")', 'socmed-articles');

我正在尝试将其转换为 vanilla javascript。 我试过了:

function populate(selector, snippet, location) {
    var snippet = (document.querySelector('snippet#' + snippet).children)[0].cloneNode(true); //printing snippet yields a span with a class socmed-articles
    document.querySelector(selector).append(snippet);
}

但我得到Uncaught DOMException: Failed to execute 'querySelector' on 'Document': 'section#main articles article:not(".ad")' is not a valid selector

CSS 选择器:not不接受" ,需要删除它们才能使用该选择器。

document.querySelector('section#main articles article:not(.ad)');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM