我想我的语法有问题。我想让它说如果 url 的 indexof 425 或者 url 的 indexof 297 不要运行以下。这仅适用于做一个: if (document.location.href.indexOf('425') === -1){ 但是当我尝试添加第二个索引时它不起作用,这是我尝试过的//attempt 1 if (document.location.href.indexOf('425') === -1 || document.location.href.indexOf('297') === -1){ }//attempt 2 if ((document.location.href.indexOf('425')) === -1 || (document.location.href.indexOf('297')) === -1)){ }
2 回答

波斯汪
TA贡献1811条经验 获得超4个赞
我想让它说如果 url 的 indexof 425 或者 url 的 indexof 297 不要运行以下。
或者换一种说法,如果该URL不具有425和 不具有297,请执行以下操作:
if (document.location.href.indexOf('425') === -1 && document.location.href.indexOf('297') === -1){
=== -1
表示没有找到。
但是现在,您可以使用includes
(如果您需要支持 IE ,则可以使用IE 的 polyfilling):
if (!document.location.href.includes('425') && !document.location.href.includes('297')){

子衿沉夜
TA贡献1828条经验 获得超3个赞
你需要一个逻辑 AND&&,因为两个部分都必须是true
if (
document.location.href.indexOf('425') === -1 &&
document.location.href.indexOf('297') === -1
) {
// ...
}
对于多个值,您可以使用包含不需要的部分的数组并Array#every用于检查。
if ['425', '297'].every(s => !document.location.href.includes(s))) {
// ...
}
添加回答
举报
0/150
提交
取消