使用jQuery存储点击的链接状态? [英] Store a clicked link state using jQuery?

查看:134
本文介绍了使用jQuery存储点击的链接状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('#nav a').click(function () {  
    $('#nav a').removeClass('current');  
    $(this).addClass('current');
    return false;  
});

我的HTML是:

<ul id="nav">
<li><a class="current hoc" href="#spread1-anchor">Home</a> /</li>
<li><a class="bgc" href="#spread2-anchor">Background</a> /</li>
<li><a class="apc" href="#spread3-anchor">Approach</a> /</li>
<li><a class="clc" href="#spread4-anchor">Client</a> /</li>
<li><a class="sec" href="#spread5-anchor">Services</a> /</li>
<li><a class="coc" href="#spread5-anchor">Contact</a></li>
</ul>

如果有帮助,我的网址是: Karls示例网站

And if it helps, my URL is: Karls Example site

使用上述jQuery代码 - 如何存储最后点击的链接用户刷新页面?我是一个初学者jQuery,即使我已经看到了答案,大多数是使用滑块或手风琴,所以太复杂,我不明白在这个阶段的学习限制! :)

Using the above jQuery code – how would I store the last clicked link when the user refreshes the page? Im a beginner with jQuery and even though I have seen answers out there, most of them are to do with sliders or accordions and so are much too complicated for me to understand at this stage of my learning curb! :)

有人可以帮助,也许显示如何实现代码或插​​件(如果需要的话,我在网上旅行时看到了一个cookies插件) / p>

Please could someone help and perhaps show how to implement the code or plugin too if one is required (I saw a cookies plugin on my travels around the net)

推荐答案

您的连结是否有ID?如果是这样,只需将ID存储在Cookie中,点击。所以,像这样:

Do your links have IDs? If so, just store the ID in a cookie when clicked. So, something like this:

$('#nav a').click(function () {
  $.cookie('lastclicked',this.id);
  $('#nav a').removeClass('current');  
  $(this).addClass('current');
  return false;  
});

使用jQuery cookie插件减少了直接设置cookie的烦恼因素。在网页加载时,查找Cookie(请参阅文档),读取其值(如果存在),现在您知道最后一次点击的链接的ID。

Using the jQuery cookie plugin reduces the annoyance factor of setting the cookie directly. On page load, look for the cookie (see documentation), read its value if present, and now you know the ID of the last clicked link.

这篇关于使用jQuery存储点击的链接状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆