点击div

时间:2015-05-07 08:30:10

标签: javascript jquery cookies

在第一页加载时,设置了一个cookie,其值为0.

基本上每次点击div时,此人(仅适用于访客)获得1 EXP,因此每次点击div +1时我都需要更改Cookie值。页面将不会刷新,因此需要存储cookie并将其显示为现场,即“您已获得50 EXP”,然后单击“您已获得51 EXP”而无需刷新。但是,如果它们也刷新,那么价值也需要保持不变。

我在stackoverflow上找到了一些代码,但我不确定如何关联它:

$(document).ready(function(){

    //set exp cookie
    setCookie("exp", '0');

    //getting the value of exp cookie and assigning it to a variable
    var expCookieCount = getCookie("exp");

    $("#div").one("click",function(){

        expCookieCount++;

        //set the incremented value of expCookieCount variable to exp cookie
        setCookie("exp",expCookieCount);

        // assigning the value of expCookiecount to variable 123
        var 123 = getCookie("exp");

    });

});

2 个答案:

答案 0 :(得分:1)

  1. 您需要包含a valid cookie script或使用jQuery cookie plugin。 JS和jQuery都没有对cookie的原生支持。

  2. 如果cookie不存在,您需要初始化它:

  3. 喜欢这个

    FIDDLE

    function showExp(exp) {
      $("#someSpan").text("You have "+exp+" point"+(exp==1?"":"s"));
    }
    
    $(function(){
      //set exp cookie
      var exp = $.cookie("exp");
      exp = (exp)?parseInt(exp,10):0;
      showExp(exp);
    
      //getting the value of exp cookie and assigning it to a variable
      $("#div").one("click",function(){ // use .on if you want every click to count
        exp++;
        //set the incremented value of expCookieCount variable to exp cookie
        $.cookie("exp",exp,{ expires: 30, path: '/' }); // expiry date 30 days
        showExp(exp);
      });
    });
    

答案 1 :(得分:0)

func
onCallbackWithTargetViewControllerName(
    targetViewControllerName: String?
,   andMetaData: [ NSObject: AnyObject ]?
){
}

这部分代码会在每次刷新时将setCookie("exp", '0'); 设置为0。在将用户设置为零之前,您应首先检查用户是否已设置exp cookie。

exp