jquery为每个链接保存多个cookie

发布于 2024-12-07 16:09:15 字数 1293 浏览 0 评论 0原文

我的页面中有很多链接,请单击每个“投票”到该链接的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2024-12-14 16:09:15

更改 cookie 名称可能会解决问题。

var cn = cookieName + $(this).attr("id");
$.cookie(cn, cookieOptions);
// save cookie depends on each link id. 
$("#" + $.cookie(cn)).addClass('voted');

更新:添加此更新是为了回答您更新后的问题:

function checkIfAllClicked(){
    var flag = false;
    $(".up").each(function(){
        var f = $(this).data("isClicked");
        if(f == true){
            flag = true;
        }
        else{
            flag = false;
        }
    });
    if(flag){
        // refresh the page or call any-other function you want
        document.location.href = "any-url";
    }
}

$(".up").live('click',function(e){
    e.preventDefault();    
    $.cookie(cookieName, $(this).attr("id"), cookieOptions);
    // save cookie depends on each link id. 
    $("#" + $.cookie(cookieName)).addClass('voted');
    $(this).data("isClicked", true);
    checkIfAllClicked();
});

change the cookie-name may resolve the problem.

var cn = cookieName + $(this).attr("id");
$.cookie(cn, cookieOptions);
// save cookie depends on each link id. 
$("#" + $.cookie(cn)).addClass('voted');

UPDATE: this update added to answer to your updated question:

function checkIfAllClicked(){
    var flag = false;
    $(".up").each(function(){
        var f = $(this).data("isClicked");
        if(f == true){
            flag = true;
        }
        else{
            flag = false;
        }
    });
    if(flag){
        // refresh the page or call any-other function you want
        document.location.href = "any-url";
    }
}

$(".up").live('click',function(e){
    e.preventDefault();    
    $.cookie(cookieName, $(this).attr("id"), cookieOptions);
    // save cookie depends on each link id. 
    $("#" + $.cookie(cookieName)).addClass('voted');
    $(this).data("isClicked", true);
    checkIfAllClicked();
});
薔薇婲 2024-12-14 16:09:15

解决了。此处参考: Jquery - 保存多个类的状态div # 到 cookie?

与所有人分享答案。

$(document).ready( function() {
    $(function() {
        var cookieName = 'up_';

        $('.up').each(function() {
            var id = $(this).attr('id'), cookie = cookieName + id;

            if ($.cookie(cookie) !== 'ture') {
                $(this).addClass('voted');
            }
        }).live('click', function(e) {
            e.preventDefault();
            $.cookie(cookieName + $(this).attr('id'), true);
        });
    });
});

Solved. refrence here: Jquery - save class state for multiple div #'s to a cookie?

share the answer to the all.

$(document).ready( function() {
    $(function() {
        var cookieName = 'up_';

        $('.up').each(function() {
            var id = $(this).attr('id'), cookie = cookieName + id;

            if ($.cookie(cookie) !== 'ture') {
                $(this).addClass('voted');
            }
        }).live('click', function(e) {
            e.preventDefault();
            $.cookie(cookieName + $(this).attr('id'), true);
        });
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文