帮忙写一段html个js的代码吧!比较简单的,我好学习一下!

2025-06-26 16:37:34
推荐回答(1个)
回答1:

如下代码:





demo


var count = 0; //人数

//写 Cookie
function setCookie(name,value) {
var Days = 30; 
var exp = new Date(); 
exp.setTime(exp.getTime() + Days*24*60*60*1000); 
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

//读取 cookie
function getCookie(name) { 
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)) {
return unescape(arr[2]); 
} else {
return null; 
}


//人数+1
function increase() {

setCookie("count", ++count);

var msg = document.getElementById("msg");
msg.innerText = "人数:" + count;
}
//人数-1
function decrease() {

setCookie("count", --count);

var msg = document.getElementById("msg");
msg.innerText = "人数:" + count;
}

window.onload = function() {
//页面加载时从cookie加载人数
count = getCookie("cookie");
if(count == null) {
count = 0;
}
var msg = document.getElementById("msg");
msg.innerText = "人数:" + count;
}



人数:0
人数+1
人数-1


只放到Web服务器上时,Cookie才会生效。