为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

温温酱 2021-06-15 08:23:12
我想我的语法有问题。我想让它说如果 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')){


查看完整回答
反对 回复 2021-06-18
?
子衿沉夜

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))) {

    // ...

}


查看完整回答
反对 回复 2021-06-18
  • 2 回答
  • 0 关注
  • 193 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信