Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何检测链接是否已被单击,即使页面已重定向_Jquery_Css - Fatal编程技术网

Jquery 如何检测链接是否已被单击,即使页面已重定向

Jquery 如何检测链接是否已被单击,即使页面已重定向,jquery,css,Jquery,Css,我想创建一个菜单栏,通过更改菜单的背景色,它将保留有关单击哪个菜单的信息 <div class="dashboard_navigation"> {% for header in headers %} <div class="navigation_header" name="Home"> <a id='a' href="{{ header.url }}">{{ header.name }}<

我想创建一个菜单栏,通过更改菜单的背景色,它将保留有关单击哪个菜单的信息

<div class="dashboard_navigation">
    {% for header in headers %}
             <div class="navigation_header" name="Home">
                <a id='a' href="{{ header.url }}">{{ header.name }}</a>

                <div class="navigation_subheader">

                        {% for subheader in subheaders %}
                            {% if header.id == subheader.parent.id %}
                                <div>
                                    <a href="{{ subheader.url }}" >{{ subheader.name }}</a> 
                                </div>
                            {% endif %}
                        {% endfor %}
                </div>  
             </div>     
    {% endfor %}

我做错了什么?问题中提供的信息是否足够,或者我是否应该添加一些内容?

当您单击链接时,页面将被重定向,因此即使您将单击事件处理程序附加到其父级
div
上,它也不会工作,因为页面将被重定向

您可以在当前用户会话的
cookie
中存储单击的链接。读取页面加载时的cookie值,并相应地设置背景色

您可以在这里查看jquerycookie插件

每个
div.navigation\u标题都有一个唯一的名称,比如“Home”,对吗?(如果他们没有,那就给他们一个!)

服务器还应该知道每个名称对应的页面。因此,它将为该页面编写一个样式

例如,对于主页页面,它会写:

div.navigation_header[name=Home] a {
    background-color: red;
}


否则,您将不得不使用cookies或URL参数。

如果我不是因为单击了链接,而是因为直接访问了URL,那该怎么办?服务器应根据用户所在的当前页面构建并突出显示导航。这不是JavaScript真正的工作。
“我应该添加一些东西吗”
--是的,如果您显示了这么多服务器代码,请适当地标记问题。我在本例中添加了ASP标记,对吗?
div.navigation_header[name=Home] a {
    background-color: red;
}