[英]Url parameter and specific conditions
我有这个网址
http://www.example.com/ln-en/Category/Product/Product-A/Details
如果我使用这个if条件
(window.location.href.split("/")[6] !== "Product-A" || window.location.href.split("/")[6] !== "Product-B" || window.location.href.split("/")[6] !== "Product-C" || window.location.href.split("/")[6] !== "Intro" || window.location.href.split("/")[6] !== "vol-A-Free" || window.location.href.split("/")[6] !== "vol-B")
我认为结果true
但是如果我只使用这个
window.location.href.split("/")[6] !== "Product-A"
我以我本来希望提供的第一个条件为false
。 有什么事吗
window.location.href.split("/")[6]
是"Product-A"
。
因此:
window.location.href.split("/")[6] !== "Product-A"
为false
和
window.location.href.split("/")[6] !== "Product-B"
为true
和
window.location.href.split("/")[6] !== "Product-"A" || window.location.href.split("/")[6] !== "Product-B"
是相同的
false || true
这是true
。
您可能想在测试中使用&&
条件。
该表达式将始终返回true
因为总是存在|
之一。 (或)符合条件的标准。 您可以使用数组indexOf
代替:
var products =["Product-A", "Product-B", "Product-C", "Intro", "vol-A-Free", "Vol-B"];
console.log(products.indexOf(window.location.href.split("/")[6]));
要么
console.log(products.indexOf(window.location.href.split("/")[6])>-1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.