javascript - 如果 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)){
}
解决方案
我想说如果 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')){
推荐阅读
- shell - shell脚本如何读取批处理测试文件夹中的数据
- python - 熊猫应用多个大于和小于按特定列分组行
- hadoop - 如何读取子工作流程中的配置属性(单独的 xml 文件)?
- c# - 耐用功能 - 活动似乎停止
- powershell - 对未知列数的 CSV 进行分组和求和
- r - 如何在 R 中将同一“分组依据”中的数据分开?
- firefox - 通过 Jenkins 使用 docker 容器在 Firefox 浏览器中运行 TestCafe
- c# - Unity 3D C# Levels 锁定和解锁
- visual-studio - Visual Studio 2017 中的新项目中缺少位置
- javascript - 如何回发多选列表中的所有选定项目,以便将其包含在我的视图模型中?