Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Location.pathname.indexOf不使用';或';( || )_Javascript_Conditional Statements - Fatal编程技术网

Javascript Location.pathname.indexOf不使用';或';( || )

Javascript Location.pathname.indexOf不使用';或';( || ),javascript,conditional-statements,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
使条件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){}
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) {}

我已经做了很多次了,由于某种原因,这段代码不起作用。我想知道我是否在代码中遗漏了一些东西,或者是其他什么东西?

基本上你是这么说的:

var a = 0;
var b = 1;
if (a != 0 || b != 0) {};
这等于

if (!(a == 0 && b == 0)) {};
但是,您实际上希望:

if (!(a == 0 || b == 0)) {};
这等于:

if (a != 0 && b != 0) {};

蒂姆已经回答了这个问题,但别忘了:

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

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

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


啊,当然。我的逻辑是错误的。我知道这很基本,谢谢你的快速回答。如果允许的话,我会在10分钟内接受。谢谢