function goodplus(gindex){
flag = 1;
num = parseInt(span.item(gindex-1).innerHTML);
if(checkcookie(gindex) == true){
num = num + 1;
senddata(gindex); //通过Ajax修改页面上的数据
}else{
alert("你已经点过赞咯!")
}
}
<script type="text/javascript">
var span = document.getElementsByTagName('span');
var num;
var flag = 0;
for(var i = 1; i < span.length + 1; i++){
senddata(i);
}
function goodplus(gindex){
flag = 1;
num = parseInt(span.item(gindex-1).innerHTML);
if(checkcookie(gindex) == true){
num = num + 1;
senddata(gindex);
}else{
alert("你已经点过赞咯!")
}
}
function senddata(aindex){
var xmlhttp;
var txt;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
txt = xmlhttp.responseText;
var cookieindex = aindex - 1;
document.getElementsByTagName('span').item(cookieindex).innerHTML = txt;
}
}
xmlhttp.open("GET","/ajax/json/index.php?num=" + num + '&flag=' + flag + '&aindex=' + aindex,true);
xmlhttp.send();
}
//判断是否已经存在了cookie
function checkcookie(gindex){
var thiscookie = 'sdcity_foodmap_goodplus' + gindex;
var mapcookie = getCookie(thiscookie)
if (mapcookie!=null && mapcookie!=""){
return false;
}else {
setCookie(thiscookie,365);
return true;
}
}