2 回答
TA贡献1810条经验 获得超4个赞
<div class="counter8">
<label id="blank"></label>
<div class="operations">
<a href="javascript:void(0)" onclick="dec_num8()" class="dec opr8">⇦</a>
<span class="count8">0</span>
<a href="javascript:void(0)" onclick="inc_num8()" class="inc opr8">⇨</a>
</div>
</div>
<script>
var add=document.querySelector(".inc");
var sub=document.querySelector(".dec");
let counter8=document.querySelector(".count8");
function inc_num8(){
var temp8=parseInt(counter8.innerText) >> 0;
counter8.innerText=Math.min(12,temp8+1);
}
function dec_num8(){
var temp8=parseInt(counter8.innerText) >> 0;
counter8.innerText=Math.max(0,temp8-1);
}
</script>
如果你想使用if分支来修复你的代码,你可以这样做:
if (temp8 >= 12) {
temp8 = 12;
} else {
temp8++;
}
counter8.innerText=temp8;
或者在一行上:
对于最少的一行,只有翻转版本temp8 = temp8 >= 12 ? temp8 : temp8+1
TA贡献1853条经验 获得超9个赞
您并没有真正限制减号按钮上的最小值计数器。而且,只有在检查条件后才能增加它。所以举个例子:
function inc_num8(){
var temp8=parseInt(counter8.innerText) >> 0;
if (temp8 >= 12) { // here changed to >=
counter8.innerText = 12; //also use innerText, not value
} else {
counter8.innerText=temp8+1;
}
}
function dec_num8(){
var temp8=parseInt(counter8.innerText) >> 0;
if(temp8 <= 0){ //here changed to <=
counter8.innerText = 0;
}else{
counter8.innerText=temp8-1;
}
}
添加回答
举报