简体   繁体   English

<a>重新载入页面后,</a>标记<a>失去背景</a>

[英]Tag <a> lose its background after page reloads

i'm trying to assign a background to my elements with javascript, but the tags keep the background only during the page reloading, for then switching back to their normal background. 我正在尝试使用javascript为我的元素分配背景,但是这些标记仅在页面重新加载期间保留背景,然后切换回其常规背景。 Here is my code: 这是我的代码:

Javascript: Javascript:

$(document).ready(function(){
    $(".container_menu ul li a").click(function() {
        $(this).addClass("active").siblings().removeClass("active");
    });
});

Css: CSS:

.container_menu ul li a.active{
    background-color: rgba(255, 255, 255, 0.156863);
}

Html: HTML:

<ul>
    <li><a class="" href="user">Home</a></li>
    <li><a class="" href="user/gallery">Gallery</a></li>
    <li><a class="" href="user/about">About the autor</a></li>
    <li><a class="" href="user/login">Manage</a></li>
</ul>

Thanks 谢谢

The reason is that you are using .active which means that the background will only be applicable while it is active. 原因是您使用的是.active,这意味着背景仅在处于活动状态时才适用。 https://jsfiddle.net/1p5zzwrf/1/ https://jsfiddle.net/1p5zzwrf/1/

.container_menu ul li a.active{
    background-color: rgba(255, 255, 255, 0.156863);
}

.container_menu ul li a{
    background-color: rgba(255, 111, 255, 0.156863);
}

If you want to show the user where he's browsing at the moment you first have to set active class on the link. 如果要在当前时刻向用户显示他正在浏览的位置,则必须首先在链接上设置active类。

So to do that: 为此:

index.html

<ul>
    <li><a class="active" href="user">Home</a></li>
    <li><a class="" href="user/gallery">Gallery</a></li>
    <li><a class="" href="user/about">About the autor</a></li>
    <li><a class="" href="user/login">Manage</a></li>
</ul>

gallery.html

<ul>
    <li><a class="" href="user">Home</a></li>
    <li><a class="active" href="user/gallery">Gallery</a></li>
    <li><a class="" href="user/about">About the autor</a></li>
    <li><a class="" href="user/login">Manage</a></li>
</ul>

And so on. 等等。

If you got a server scripting language that would make it easier for you. 如果您使用的服务器脚本语言可以简化您的工作。

Your javascript code is altering the page after it has been loaded. 您的JavaScript代码正在加载页面后更改页面。 So when you reload the page, it goes back to its initial state. 因此,当您重新加载页面时,它会返回到其初始状态。

What you are trying to do is hardly achievable without some server side code (unless you are building a single page application but I don't think so). 没有一些服务器端代码,您试图做的事几乎是不可能实现的(除非您正在构建一个单页应用程序,但我不这么认为)。 Your server needs to know what is the active page so it renders it with your link having the active class. 您的服务器需要知道什么是活动页面,以便它通过具有active类的链接来呈现它。

If you can't or do not want to deal with the server solution, you'll have to find out what is the active page in your javascript code. 如果您不能或不想使用服务器解决方案,则必须找出javascript代码中的活动页面。 It means that you'll need to detect it and then add the class to the proper element. 这意味着您需要检测它,然后将类添加到适当的元素中。 This operation can take time (ie while the javascript file loads) and the link might first appears without its active state for a short period. 此操作可能会花费一些时间(即,在加载javascript文件时),并且该链接可能会在短时间内没有其活动状态的情况下首次出现。 Anyway, here is a possible way to detect the active page: 无论如何,这是一种检测活动页面的可能方法:

var link = document.getElementById('link-user-about');
if (/user\/about\/$/.test(window.location.href)) {
    link.className = 'active';
}

Note: this is not the complete solution but a good hint ;) 注意:这不是完整的解决方案,而是一个很好的提示;)

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

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