jquery为每个链接保存多个cookie
我的页面中有很多链接,请单击每个“投票”到该链接的 addClass
链接。然后将效果保存到cookie中。这样下次刷新网络浏览器时,它将保存 addClass
事件。
我的代码在这里,但它只会保存 1 个 cookie。我的意思是,如果我点击链接1
,链接2
。它总是记住最后一次点击。刷新网络浏览器。仅link 2
addClass
“投票”。 link1
错过了 addClass
事件。那么如何为每个链接保存多个cookie呢?谢谢。
<script type="text/javascript" src="script/jquery.js"></script>
<script type="text/javascript" src="script/cookie.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var cookieName = 'up';
var cookieOptions = {expires: 7, path: '/'};
$("#" + $.cookie(cookieName)).addClass('voted');
$(".up").live('click',function(e){
e.preventDefault();
$.cookie(cookieName, $(this).attr("id"), cookieOptions);
// save cookie depends on each link id.
$("#" + $.cookie(cookieName)).addClass('voted');
});
});
</script>
<a href="javascript:void(0)" id="m12345678" class="up">link 1</a>
<a href="javascript:void(0)" id="m12345679" class="up">link 2</a>
<a href="javascript:void(0)" id="m12345680" class="up">link 3</a>
上传我的代码:jsfiddle
I have lots of links in the page, click each one addClass
'voted' to this link. then save the effection into cookies. so that next time refresh web browser, it will save the addClass
event.
My code here, but it will only save 1 cookie. I mean if I clicked link 1
, link 2
. it always remember the last click. refresh the web browser. only link 2
addClass
'voted'. link1
missed addClass
event. so how to save multiple cookie for every link? Thanks.
<script type="text/javascript" src="script/jquery.js"></script>
<script type="text/javascript" src="script/cookie.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var cookieName = 'up';
var cookieOptions = {expires: 7, path: '/'};
$("#" + $.cookie(cookieName)).addClass('voted');
$(".up").live('click',function(e){
e.preventDefault();
$.cookie(cookieName, $(this).attr("id"), cookieOptions);
// save cookie depends on each link id.
$("#" + $.cookie(cookieName)).addClass('voted');
});
});
</script>
<a href="javascript:void(0)" id="m12345678" class="up">link 1</a>
<a href="javascript:void(0)" id="m12345679" class="up">link 2</a>
<a href="javascript:void(0)" id="m12345680" class="up">link 3</a>
Upload my code in: jsfiddle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改 cookie 名称可能会解决问题。
更新:添加此更新是为了回答您更新后的问题:
change the cookie-name may resolve the problem.
UPDATE: this update added to answer to your updated question:
解决了。此处参考: Jquery - 保存多个类的状态div # 到 cookie?
与所有人分享答案。
Solved. refrence here: Jquery - save class state for multiple div #'s to a cookie?
share the answer to the all.