Javascript document.queryselectoral:not与多个:not

Javascript document.queryselectoral:not与多个:not,javascript,css,Javascript,Css,事先抱歉-此问题存在如此多的差异,但没有解决我的问题。我读了太多的变化,现在我完全不知道如何做到这一点 我有一个Javascript函数,它将UTM参数附加到所有非现场链接中,但具有特定类的链接除外: // Set the domain/URL of website. var myDomain = "domain.com"; // Grab all links (anchor tags) var links = document.querySelectorAll('a:not(.this-c

事先抱歉-此问题存在如此多的差异,但没有解决我的问题。我读了太多的变化,现在我完全不知道如何做到这一点

我有一个Javascript函数,它将UTM参数附加到所有非现场链接中,但具有特定类的链接除外:

// Set the domain/URL of website.

var myDomain = "domain.com";

// Grab all links (anchor tags)
var links = document.querySelectorAll('a:not(.this-class)');

// Loop through all links
Array.prototype.forEach.call(links, function (link) {

// If a link goes offsite (not to my myDomain above)
if ( link.href.indexOf(myDomain) < 0 ) {

// Take the href and append the UTM parameters
link.href += '?utm_source=CampaignSource&utm_medium=CampaignMedium&utm_term=CampaignTerm&utm_content=CampaignContent&utm_campaign=CampaignName';
    }   
})
我尝试过无数的语法和格式,但就是不能让它正常工作。比如

var links = document.querySelectorAll('a:not(.this-class),ul:not(.another_class li a)');
还有太多的要在这里列出。选择器ul。另一个_类li a,只有ul有一个类-li和a都没有

所以,我的问题是关于正确的:非选择器语法和正确的语句语法。应该是

('a:not(.this-class), ul:not(.another_class li a)');


还是别的什么?我感谢您的专业知识。

单用一个选择器不可能做到简洁,因为。我认为在这里最好的做法是过滤掉
ul。然后再过滤掉另一类li a

for (const a of document.querySelectorAll('a:not(.this-class)')) {
  if (!a.matches('ul.another_class li a')) {
    // manipulate the href
  }
}

你可以先迭代
ul.other_class li a
元素,然后将
这个类添加到元素中。谢谢Phil。给定上面的代码块,情况如何?
document.queryselectoral('ul.other_class li a').forEach(a=>a.classList.add('this-class'))
然后您可以像上面一样使用代码谢谢Phil!那确实管用!最明显的警告是.this类样式的应用。如果我想不出下面的解决方案,我就用这个。谢谢你!抱歉,原来的帖子包含了整个功能。那么,我是否可以简单地将Array.prototype.forEach移动到for语句中的//操纵href?不,上面
for
块中的
a
a
href
你想操纵它的
href
就可以检查它的
href
并对其进行操作,例如
a.href
哦,对了,我想从操纵中排除该选择器。因此,对于除a.this-class和ul.another_class li a以外的所有场外链接。仍然有效吗?这就是逻辑所做的-
a:not(.this class)
选择
a
s而不是
。这个class
,并且
匹配的
测试确保,在这些中,只有那些不匹配的
ul。另一个
被操纵不确定你是否看到了上面的Phil解决方案,它是有效的。谢谢你澄清了逻辑,现在有道理了。我只是不知道如何在上一个解决方案的上下文中操作href。我是否删除该函数并附加UTM?如果是,如何?
('a:not(.this-class), ul.another_class:not(li a)');
for (const a of document.querySelectorAll('a:not(.this-class)')) {
  if (!a.matches('ul.another_class li a')) {
    // manipulate the href
  }
}