用js如何实现点击加或减 其数值做相应的改变
<divclass="right"><spanclass="add">+</span><spanclass="math">1</span><spanclass="jian...
<div class="right"> <span class="add">+</span> <span class="math">1</span> <span class="jian">-</span></div>点击加减符号 数字做相应的增或减 (+1&-1)
展开
展开全部
<script>
var span = document.getElementsByTagName("span");
for (var i = 0; i < span.length; i++) {
(function (i) {
span[i].addEventListener('click', function () {
var value = this.innerHTML;
var math = document.getElementsByClassName("math")[0];
var mvalue = math.innerHTML;
if (value == '+') {
mvalue++;
math.innerHTML = mvalue;
}
else if (value == '-') {
if (mvalue == '1') {
return;
}
mvalue--;
math.innerHTML = mvalue;
}
})
})(i);
}
</script>
每次加1,给每个span标签添加点击事件,在里面判断点的是+还是-做后在赋值
var span = document.getElementsByTagName("span");
for (var i = 0; i < span.length; i++) {
(function (i) {
span[i].addEventListener('click', function () {
var value = this.innerHTML;
var math = document.getElementsByClassName("math")[0];
var mvalue = math.innerHTML;
if (value == '+') {
mvalue++;
math.innerHTML = mvalue;
}
else if (value == '-') {
if (mvalue == '1') {
return;
}
mvalue--;
math.innerHTML = mvalue;
}
})
})(i);
}
</script>
每次加1,给每个span标签添加点击事件,在里面判断点的是+还是-做后在赋值
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询