首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查jquery中的多个条件

问检查jquery中的多个条件
EN

Stack Overflow用户
提问于 2020-03-21 04:12:29
回答 1查看 25关注 0票数 0

它检查多个条件,没有问题。但不会再往前走了。当我删除多个条件时,脚本可以很好地工作。我的代码出了什么问题?

代码语言:javascript
复制
    $(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> ```
EN

回答 1

Stack Overflow用户

发布于 2020-03-22 08:34:52

代码格式不正确。indexof()总是返回一个int,因此检查(indexof())是否总是为真。您需要对每个检查进行比较,如下所示:

代码语言:javascript
复制
$(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");
        }         
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60781041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档