单击 div 时将 +1 添加到 cookie 值
Adding +1 to a cookie value on click of a div
在第一个页面加载时设置一个 cookie,其值为 0。
基本上每次点击 div
这个人(这只适用于客人)都会获得 1 EXP,所以我需要在每次点击 div
+1 时更改 cookie 值.该页面不会刷新,因此它需要存储 cookie 并实时显示它,即 "You have earned 50 EXP",然后单击 "You have earned 51 EXP" 而不刷新。但是,如果它们也刷新,该值也需要保持不变。
我在 Whosebug 上找到了一些代码,但我不确定如何关联它:
$(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");
});
});
setCookie("exp", '0');
这部分代码会在每次刷新时将 exp
设置为 0。在将其设置为零之前,您应该首先检查用户是否已经设置了 exp
cookie。
$(document).ready(function(){
var exp = getCookie("exp");
// Im assuming your getCookie function returns "" when cookie is not present
if(exp===""){
//set exp cookie
setCookie("exp", '0');
exp = 0;
} else {
// Just to make sure it's an integer I had to use parseInt()
exp = parseInt(exp)
}
$("#div").one("click",function(){
exp++;
//set the incremented value of expCookieCount variable to exp cookie
setCookie("exp", exp);
});
});
您需要包含 a valid cookie script or use the jQuery cookie plugin。 JS 和 jQuery 都没有对 cookie 的原生支持。
cookie不存在需要初始化:
像这样
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);
});
});
在第一个页面加载时设置一个 cookie,其值为 0。
基本上每次点击 div
这个人(这只适用于客人)都会获得 1 EXP,所以我需要在每次点击 div
+1 时更改 cookie 值.该页面不会刷新,因此它需要存储 cookie 并实时显示它,即 "You have earned 50 EXP",然后单击 "You have earned 51 EXP" 而不刷新。但是,如果它们也刷新,该值也需要保持不变。
我在 Whosebug 上找到了一些代码,但我不确定如何关联它:
$(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");
});
});
setCookie("exp", '0');
这部分代码会在每次刷新时将 exp
设置为 0。在将其设置为零之前,您应该首先检查用户是否已经设置了 exp
cookie。
$(document).ready(function(){
var exp = getCookie("exp");
// Im assuming your getCookie function returns "" when cookie is not present
if(exp===""){
//set exp cookie
setCookie("exp", '0');
exp = 0;
} else {
// Just to make sure it's an integer I had to use parseInt()
exp = parseInt(exp)
}
$("#div").one("click",function(){
exp++;
//set the incremented value of expCookieCount variable to exp cookie
setCookie("exp", exp);
});
});
您需要包含 a valid cookie script or use the jQuery cookie plugin。 JS 和 jQuery 都没有对 cookie 的原生支持。
cookie不存在需要初始化:
像这样
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);
});
});