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

Javascript 如果url的索引为x或y,则不要执行以下操作,javascript,Javascript,我想我在这方面的语法有问题。我想让它说如果url有indexof 425或者如果url有indexof 297,请不要运行以下命令。这只适用于做一件事: if (document.location.href.indexOf('425') === -1){ 但当我尝试添加第二个索引时,它不起作用,下面是我尝试过的 //attempt 1 if (document.location.href.indexOf('425') === -1 || document.location.h

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

}
我想让它说如果url有indexof 425或者如果url有indexof 297,请不要运行以下命令

或者换句话说,如果url没有425和297,请执行以下操作:

if (document.location.href.indexOf('425') === -1 && document.location.href.indexOf('297') === -1){
=-1表示找不到它

但是现在,如果您需要支持IE,您可以使用polyfilling for IE:

if (!document.location.href.includes('425') && !document.location.href.includes('297')){
我想让它说如果url有indexof 425或者如果url有indexof 297,请不要运行以下命令

或者换句话说,如果url没有425和297,请执行以下操作:

if (document.location.href.indexOf('425') === -1 && document.location.href.indexOf('297') === -1){
=-1表示找不到它

但是现在,如果您需要支持IE,您可以使用polyfilling for IE:

if (!document.location.href.includes('425') && !document.location.href.includes('297')){
你需要一个答案,因为这两部分都必须是真的

对于多个值,可以使用包含不需要的部分的数组进行检查

if ['425', '297'].every(s => !document.location.href.includes(s))) {
    // ...
}
你需要一个答案,因为这两部分都必须是真的

对于多个值,可以使用包含不需要的部分的数组进行检查

if ['425', '297'].every(s => !document.location.href.includes(s))) {
    // ...
}

=-1,带indexOf,表示搜索的指针在原始字符串中不存在。您可能希望:>-1,如果您希望在url具有搜索指针时执行操作。&&而不是| |和isntead of OR===-1,带有indexOf,表示搜索指针在原始字符串中不存在。您可能希望:>-1,如果您希望在url具有搜索指针的情况下执行操作,&&而不是| |和isnteadOR@Jenny-我们有时都会被这些东西绊倒-@珍妮-我们有时都会被这些东西绊倒-