摘要:html代码JS代码 html代码JS代码 

小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

html代码

 <p>       <p>单价:<span>30</span></p>       <input type="button" value="-" class="sub">       <input type="text" value="1">       <input type="button" value="+" class="add">       <p>总价:<span>30</span></p>   </p>   <p>       <p>单价:<span>666</span></p>       <input type="button" value="-" class="sub">       <input type="text" value="1">       <input type="button" value="+" class="add">       <p>总价:<span>666</span></p>   </p>   <p>       <p>单价:<span>888</span></p>       <input type="button" value="-" class="sub">       <input type="text" value="1">       <input type="button" value="+" class="add">       <p>总价:<span>888</span></p>   </p>  

JS代码 

 var aAdd = document.querySelectorAll(".add");       var sub = document.querySelectorAll(".sub");       for (var i = 0; i < aAdd.length; i++) {           aAdd[i].onclick = function () {               var num = Number(this.previousElementSibling.value);               num++;               this.previousElementSibling.value = num;               var price = this.previousElementSibling.previousElementSibling.previousElementSibling.firstElementChild.innerHTML;               this.nextElementSibling.firstElementChild.innerHTML = price * num;           }       }       for (var i = 0; i < sub.length; i++) {           sub[i].onclick = function () {               var num = Number(this.nextElementSibling.value);               num--;               this.nextElementSibling.value = num;               var price = this.previousElementSibling.firstElementChild.innerHTML;               this.nextElementSibling.nextElementSibling.nextElementSibling.firstElementChild.innerHTML = price * num;           }       }  

html代码

 <p>       <p>单价:<span>30</span></p>       <input type="button" value="-" class="sub">       <input type="text" value="1">       <input type="button" value="+" class="add">       <p>总价:<span>30</span></p>   </p>   <p>       <p>单价:<span>666</span></p>       <input type="button" value="-" class="sub">       <input type="text" value="1">       <input type="button" value="+" class="add">       <p>总价:<span>666</span></p>   </p>   <p>       <p>单价:<span>888</span></p>       <input type="button" value="-" class="sub">       <input type="text" value="1">       <input type="button" value="+" class="add">       <p>总价:<span>888</span></p>   </p>  

JS代码 

 var aAdd = document.querySelectorAll(".add");       var sub = document.querySelectorAll(".sub");       for (var i = 0; i < aAdd.length; i++) {           aAdd[i].onclick = function () {               var num = Number(this.previousElementSibling.value);               num++;               this.previousElementSibling.value = num;               var price = this.previousElementSibling.previousElementSibling.previousElementSibling.firstElementChild.innerHTML;               this.nextElementSibling.firstElementChild.innerHTML = price * num;           }       }       for (var i = 0; i < sub.length; i++) {           sub[i].onclick = function () {               var num = Number(this.nextElementSibling.value);               num--;               this.nextElementSibling.value = num;               var price = this.previousElementSibling.firstElementChild.innerHTML;               this.nextElementSibling.nextElementSibling.nextElementSibling.firstElementChild.innerHTML = price * num;           }       }  

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!