javascript - 我将如何做一个等同于使用伪类查询多个选择器的 vanilla JS?

标签 javascript jquery

所以我有这个功能:

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)');

关于javascript - 我将如何做一个等同于使用伪类查询多个选择器的 vanilla JS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64451929/

相关文章:

javascript - Document.write 函数用文本替换整个页面

javascript - 返回的事件对象覆盖父对象 - javascript、easeljs

javascript - Sticky '_calc' 不是该元素的可用方法

javascript - preg 替换整个 div 或 div 内容 javascript?

javascript - 基于隐藏字段的 JQuery 条件格式

javascript - React : this. state.renderData.filter 不是一个函数

c# - 如何在 javascript 中找到父窗口的标题?

javascript - 使用 Firebug 调试 JavaScript 事件

javascript - jquery 和 javascript 不工作

javascript - Laravel 5.1 - 使用 ajax 打印评论,然后能够立即删除它