javascript - Location.pathname.indexOf 不适用于 'Or' ( || )

标签 javascript conditional-statements

我正在尝试使用 location.pathname.indexOf 让条件 jQuery 在我网站的某些页面上工作。

这个有效:

if (location.pathname.indexOf("/example/5820.htm") != 0){}

这个有效:

if (location.pathname.indexOf("/example-1/3569.htm") != 0) {}

这行不通:

if (location.pathname.indexOf("/example/5820.htm") != 0 || location.pathname.indexOf("/example-1/3569.htm") != 0) {}

我已经这样做了很多次,但出于某种原因,这段代码无法正常工作。我想知道我是否在代码中遗漏了一些小东西,或者是否还有其他东西?

最佳答案

Tim 已经回答了这个问题,但不要忘记:

.indexOf() 将在找不到字符串时返回 -1,而不是 0。

if (location.pathname.indexOf("/example/5820.htm") != 0){}

应该是:

if (location.pathname.indexOf("/example/5820.htm") != -1){}

或者:

if (location.pathname.indexOf("/example/5820.htm") >= 0){}

http://www.w3schools.com/jsref/jsref_indexof.asp

关于javascript - Location.pathname.indexOf 不适用于 'Or' ( || ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21265919/

相关文章:

arrays - Perl:对数组使用条件

javascript - 如何在 NodeJS 中的 .txt 文件中添加新行文本

javascript - 如何让我的 Div2 直接位于我的 Div1 下方

javascript - knockout JS建议

javascript - 沙盒模型的 div 与 iframe

用于检查空数组的 Azure 逻辑应用条件

c - 使用c中的条件声明变量的数据类型

php - 使用 anchor 标记提交表单以在新窗口中打开 pdf

javascript - 使用三元运算符输出字符串

sql - SQL语句中使用WHERE 1=1的目的是什么?