<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>安心牧羊人</title>
</head>
<body>
<p>全局变量计数。</p>
<button type="button" onclick="myFunction1()">-</button>
<span id="demo" style="color:red;">0</span>
<button type="button" onclick="myFunction2()">+</button>
<script>
var counter=0;
function add() {
if(counter>=10){ //设置库存最大数例如10以上不能点击加
return counter += 0;
}else{
return counter += 1;
}
return counter += 1;
}
function cor() {
if(counter===0){ //设置最小数值不能低于0 ,低于0不能点击
return counter += 0;
}else{
return counter += -1;
}
}
function myFunction1(){
document.getElementById("demo").innerHTML = cor();
}
function myFunction2(){
document.getElementById("demo").innerHTML = add();
}
</script>
</body>
</html>