如果 url 的索引为 x 或 y,则不要执行以下操作

我想我的语法有问题。我想让它说如果 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)){ 


}


温温酱
浏览 194回答 2
2回答

波斯汪

我想让它说如果 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')){

子衿沉夜

你需要一个逻辑 AND&&,因为两个部分都必须是trueif (    document.location.href.indexOf('425') === -1 &&    document.location.href.indexOf('297') === -1) {     // ...}对于多个值,您可以使用包含不需要的部分的数组并Array#every用于检查。if ['425', '297'].every(s => !document.location.href.includes(s))) {    // ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript