首页 > 解决方案 > 如果 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)){ 

}

标签: javascript

解决方案


我想说如果 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 填充):

if (!document.location.href.includes('425') && !document.location.href.includes('297')){

推荐阅读