How to detect if a link has been clicked, even though the page is redirected
I want to create a menu bar which will retain the info about which menu was clicked by changing the background color of that menu.
<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 %}
The script I am using is as follows
$(".navigation_header").live('click', function()
{
$(t开发者_运维百科his).children('a').css("background-color","red");
});
What am I doing wrong? Is the info provided in the question enough or should I add something?
When you click on the link the page is going to be redirected so even if you attached a click event handler to its parent div
it is not going to work as the page will be redirected.
You can store which link is click in a cookie
for the current user session. Read the cookie value on page load and set the background color accordingly.
You can take a look at jquery cookie plugin here http://plugins.jquery.com/project/Cookie
Each div.navigation_header
has a unique name, like "Home", right? (If they don't then give them one!)
Your server should also know which page corresponds with each name. So, it will write a style just for that page.
For example, for the Home page, it would write:
div.navigation_header[name=Home] a {
background-color: red;
}
Otherwise, you would have to use cookies or URL parameters.
精彩评论