根据加载的HTML文件更改菜单选项颜色

Change Menu Option Color Depending on the HTML File Loaded

本文关键字:菜单 选项 颜色 文件 加载 HTML      更新时间:2023-09-26

我想根据加载的页面改变菜单的颜色。我知道这是非常简单的服务器端语言的支持,但这是一个HTML项目,我进入。因此,我决定获取正在加载的HTML文件的名称并采取相应的行动,但是在这种方法中我遇到了一个奇怪的问题。让我解释一下。

我的菜单:

      <nav>
            <ul>
                <li><a class="menu-option" id="about" href="about.html">About</a></li>
                <li><a class="menu-option" id="trainer" href="#">Trainer / Consultant</a></li>
                <li><a class="menu-option" id="testimonial" href="#">Testimonials</a></li>
                <li><a class="menu-option" id="programs" href="#">Programs</a></li>
                <li><a class="menu-option" id="contact" href="#">Contact us</a></li>
            </ul>
        </nav>
在JS

$(document).ready(function() {
    $("header").load("includes/header.html");
    $("footer").load("includes/footer.html");
    var page = location.pathname.split("/").slice(-1);
    switch (page[0])
    {
        case "about.html":
            $(".menu-option").css('color', 'white');
            $("#about").css('color', '#E45C02');
            break;
        case "trainer.html":
            $(".menu-option").css('color', 'white');
            $("#trainer").css('color', '#E45C02');
            break;
        case "programs.html":
            $(".menu-option").css('color', 'white');
            $("#programs").css('color', '#E45C02');
            break;
        case "testimonial.html":
            $(".menu-option").css('color', 'white');
            $("#testimonial").css('color', '#E45C02');
            break;
        case "contact.html":
            $(".menu-option").css('color', 'white');
            $("#contact").css('color', '#E45C02');
        default:
            $(".menu-option").css('color', 'white');
            break;
    }

});

使用Firebug,我可以看到这个case正在被调用,但是about的颜色没有受到影响。所以我决定在about中添加一个警告

case "about.html":
            alert("about");
            $(".menu-option").css('color', 'white');
            $("#about").css('color', '#E45C02');
            break;

这个东西像预期的那样工作。我真的很困惑。为什么没有alert语句代码会失败?它扮演着什么角色?我遗漏了什么?我需要暂停一下吗?

我还试着把开关箱放在

$("header").ready(function(){
// Switch case here
// Still no luck !
});

这是因为.load()是异步的,这意味着你需要等到加载完成后再采取任何依赖于加载数据的操作。

您可以发送一个函数作为.load()的第二个参数,该函数将在加载数据后被调用。然后可以在该函数中运行switch语句。

的例子:

$(document).ready(function() {
    function onHeaderLoaded() {
        var page = location.pathname.split("/").slice(-1);
        switch (page[0])
        {
            case "about.html":
                $(".menu-option").css('color', 'white');
                $("#about").css('color', '#E45C02');
                break;
            case "trainer.html":
                $(".menu-option").css('color', 'white');
                $("#trainer").css('color', '#E45C02');
                break;
            case "programs.html":
                $(".menu-option").css('color', 'white');
                $("#programs").css('color', '#E45C02');
                break;
            case "testimonial.html":
                $(".menu-option").css('color', 'white');
                $("#testimonial").css('color', '#E45C02');
                break;
            case "contact.html":
                $(".menu-option").css('color', 'white');
                $("#contact").css('color', '#E45C02');
            default:
                $(".menu-option").css('color', 'white');
                break;
        }
    }
    $("header").load("includes/header.html", onHeaderLoaded);
    $("footer").load("includes/footer.html");
});