微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我该如何用伪类查询多个选择器来实现普通的JS?

如何解决我该如何用伪类查询多个选择器来实现普通的JS?

所以我有这个功能

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。