我正在尝试使用 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){}
关于javascript - Location.pathname.indexOf 不适用于 'Or' ( || ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21265919/