Javascript 添加if语句

Javascript 添加if语句,javascript,if-statement,Javascript,If Statement,我的代码如下所示: $('a').each(function(){ $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium); }); var exclude = [ "https://www.mysite.or/cat1", "https://www.a

我的代码如下所示:

$('a').each(function(){
  $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source  + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
});
var exclude = [
  "https://www.mysite.or/cat1",
  "https://www.anysite.com"
]

$('a').each(function() {
  var href = $(this).attr('href');
  if (exclude.indexOf(href) === -1) {
    $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
  }
});
该代码向站点上的所有链接添加了一些UTM参数。但现在我想排除一些特定链接:

https://www.mysite.or/cat1
https://www.mysite.or/cat2
所以我想到了:

$('a').each(function() {
  if ("href:contains('https://www.mysite.or/cat1')") {
    $(this).attr('href');
  } else if ("href:contains('https://www.mysite.or/cat1')") {
    $(this).attr('href');
  } else {
    $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
  }
});

但是它不能正常工作。

如果您创建一个排除域的列表会更好,这样以后添加更多域会更容易。例如:

$('a').each(function(){
  $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source  + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
});
var exclude = [
  "https://www.mysite.or/cat1",
  "https://www.anysite.com"
]

$('a').each(function() {
  var href = $(this).attr('href');
  if (exclude.indexOf(href) === -1) {
    $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
  }
});

正如@caramba在评论中指出的,如果您关心性能(即您有数千个
a
要迭代,或者有几十个排除的域),那么上述解决方案并不是最快的。另一种可能的解决方案(同时保持代码易于维护)是构建选择器并将其传递给jQuery:

var exclude = [
  "https://www.mysite.or/cat1",
  "https://www.anysite.com"
]

var nots = exclude.map(function(domain) {
    return ':not([href="' + domain + '"])';
}).join('');

$('a' + nots).each(function() {
  var href = $(this).attr('href');
  $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
});

如果您创建一个排除域的列表会更好,这样以后添加更多域会更容易。例如:

$('a').each(function(){
  $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source  + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
});
var exclude = [
  "https://www.mysite.or/cat1",
  "https://www.anysite.com"
]

$('a').each(function() {
  var href = $(this).attr('href');
  if (exclude.indexOf(href) === -1) {
    $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
  }
});

正如@caramba在评论中指出的,如果您关心性能(即您有数千个
a
要迭代,或者有几十个排除的域),那么上述解决方案并不是最快的。另一种可能的解决方案(同时保持代码易于维护)是构建选择器并将其传递给jQuery:

var exclude = [
  "https://www.mysite.or/cat1",
  "https://www.anysite.com"
]

var nots = exclude.map(function(domain) {
    return ':not([href="' + domain + '"])';
}).join('');

$('a' + nots).each(function() {
  var href = $(this).attr('href');
  $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
});

通过缩小选择器的范围排除所有不需要的url

$('a:not([href$="cat1"]):not([href$="cat2"])').each(function() {
  $(this).attr('href', ...);
});
$=
表示以

$('a:not([href$=“cat1”):not([href$=“cat2”))。每个(函数(){
$(this.html('changed'))
});    

通过缩小选择器的范围排除所有不需要的url

$('a:not([href$="cat1"]):not([href$="cat2"])').each(function() {
  $(this).attr('href', ...);
});
$=
表示以

$('a:not([href$=“cat1”):not([href$=“cat2”))。每个(函数(){
$(this.html('changed'))
});    

您还可以执行以下操作:

$('a:not([href^="https://www.mysite.or/cat"])')
只会选择
您还可以执行以下操作:

$('a:not([href^="https://www.mysite.or/cat"])')
只会选择


$(this.attr('href')返回属性的值,因此您缺少左侧赋值。是否可以更改if,使其仅在语句不为true时继续?像
if(href!='https://www.mysite-com/cat“&&href!=“其他站点名称”)
如果您有很多If语句,请将其改为开关盒。
$(this.attr('href')返回属性的值,因此您缺少左侧赋值。是否可以更改if,使其仅在语句不为true时继续?像
if(href!='https://www.mysite-com/cat“&&href!=“其他站点名称”)
如果您有很多If语句,请将其改为switch case。我不能使用const,因为此语言功能仅在ECMAScript 6模式或更好的模式下受支持。是否有const的替代方案?现在几乎所有浏览器都支持它:。但是如果您仍然想更改它,可以用
var
替换
const
。编辑答案做[]声明一个常量-因为我又犯了一个错误,但似乎需要注意:就性能而言,这是最差的答案<代码>1
选择了所有a标签,可能有很多,包括许多不相关的标签<代码>2
需要在所有这些a标记上循环<代码>3
在循环中,需要对所有“排除”元素进行另一个循环,这本可以在
1
步骤中避免,考虑到排除的域的数量最多只有两个,这实际上是一场几毫秒的斗争,同时失去可读性。如果与
a
s的数量相比,排除域的大量使用将非常重要,那么性能差异将变得非常重要。否则,您无论如何都要迭代几乎所有的
a
标记,并在
excluded
数组上最多迭代几次,这与构建大型选择器相比增加了可读性。我不能使用常量,因为此语言功能仅在ECMAScript 6模式或更高模式下受支持。是否有const的替代方案?现在几乎所有浏览器都支持它:。但是如果您仍然想更改它,可以用
var
替换
const
。编辑答案做[]声明一个常量-因为我又犯了一个错误,但似乎需要注意:就性能而言,这是最差的答案<代码>1
选择了所有a标签,可能有很多,包括许多不相关的标签<代码>2需要在所有这些a标记上循环<代码>3在循环中,需要对所有“排除”元素进行另一个循环,这本可以在
1
步骤中避免,考虑到排除的域的数量最多只有两个,这实际上是一场几毫秒的斗争,同时失去可读性。如果与
a
s的数量相比,排除域的大量使用将非常重要,那么性能差异将变得非常重要。否则,无论如何,您都希望迭代几乎所有的
a
标记,并在
excluded
数组上最多迭代几次,这与构建一个巨大的选择器相比增加了可读性。谢谢-但它不起作用。现在没有链接了。@Tomtom my bad,选择器上的一个输入错误。看到更新了吗。现在没有链接了。@Tomtom my bad,选择器上的一个输入错误。查看更新