简体   繁体   English

如何根据有效网址更改导航栏中的图像?

[英]How do I change the image in a navbar based on what the active url is?

What I'm trying to do is have #navbar-image-id2games be toggled when the active url is index.html . 我想做的是在活动网址为index.html时切换#navbar-image-id2games I attempted to do this with javascript like so, but it doesn't seem to be working. 我试图用javascript来做到这一点,但是似乎没有用。

HTML: HTML:

<div class="navbar-item" id="navbar-item-ID2Games">
                            <a href="index.html" id="index-url">
                                <div class="navbar-image" id="navbar-image-unhovered">
                                </div>
                                <div class="navbar-image" id="navbar-image-id2games">
                                </div>

                                <br>

                                <div class="navbar-text">
                                    ID2 Games
                                </div>
                            </a>
    </div>

Javascript: 使用Javascript:

$('#index-url').active(function() {
                  $('#navbar-image-id2games').toggle();
                  $('#navbar-image-unhovered').hide();
});

JSFiddle: https://jsfiddle.net/zxsmuaae/ JSFiddle: https ://jsfiddle.net/zxsmuaae/

You can check whether or not the user is at index.html by doing: 您可以通过执行以下操作来检查用户是否在index.html

if (window.location.href.match(/index\.html/)) {
    // toggle
}

You can check the url with an if statement, and then set the visibility to hidden if it is on a certain page. 您可以使用if语句检查url,然后将可见性设置为隐藏(如果它在特定页面上)。

var navimg = document.getElementById("navbar-image-id2games");
if(window.location.href === "http://example/index.html"){
    navimg.style.visibility = "hidden";
}

If you want it to be reverse where it is hidden only when it is not on index.html , you can replace === with !== . 如果要使其反向(仅当它不在index.html上时才隐藏),可以将===替换为!==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM