它检查多个条件,没有问题。但不会再往前走了。当我删除多个条件时,脚本可以很好地工作。我的代码出了什么问题?
$(document).ready(function () {
if (window.location.href.indexOf("car-club-insurance") || window.location.href.indexOf("motor-home-hire-insurance") || window.location.href.indexOf("van-minibus-hire-insurance") != -1) {
$("ol.breadcrumb li:nth-child(1)").addClass("active");
}
else if
(window.location.href.indexOf("FormTwo") != -1) {
$("ol.breadcrumb li:nth-child(3)").addClass("active");
}
else if
(window.location.href.indexOf("FormThree") != -1) {
$("ol.breadcrumb li:nth-child(5)").addClass("active");
}
else if
(window.location.href.indexOf("PolicyHolder") != -1) {
$("ol.breadcrumb li:nth-child(7)").addClass("active");
}
else if
(window.location.href.indexOf("Payment") != -1) {
$("ol.breadcrumb li:nth-child(9)").addClass("active");
}
});
</script> ```发布于 2020-03-22 08:34:52
代码格式不正确。indexof()总是返回一个int,因此检查(indexof())是否总是为真。您需要对每个检查进行比较,如下所示:
$(document).ready(function () {
if (window.location.href.indexOf("car-club-insurance") != -1 || window.location.href.indexOf("motor-home-hire-insurance") != -1 || window.location.href.indexOf("van-minibus-hire-insurance") != -1) {
$("ol.breadcrumb li:nth-child(1)").addClass("active");
}
else if
(window.location.href.indexOf("FormTwo") != -1) {
$("ol.breadcrumb li:nth-child(3)").addClass("active");
}
else if
(window.location.href.indexOf("FormThree") != -1) {
$("ol.breadcrumb li:nth-child(5)").addClass("active");
}
else if
(window.location.href.indexOf("PolicyHolder") != -1) {
$("ol.breadcrumb li:nth-child(7)").addClass("active");
}
else if
(window.location.href.indexOf("Payment") != -1) {
$("ol.breadcrumb li:nth-child(9)").addClass("active");
}
});https://stackoverflow.com/questions/60781041
复制相似问题