所以我有这个功能:
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');
我正在尝试将其转换为普通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)');
关于javascript - 我该如何用伪类查询多个选择器来实现普通JS?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64451929/