购物车加减按钮
程序员文章站
2022-11-21 22:50:50
分别用JavaScript,jQuery和vue做了一个简单的购物车加减按钮部分。 HTML: css(主要是去除按钮默认样式,赋予新样式): JavaScript: jQuery: vue版 HTML: vue: ......
分别用javascript,jquery和vue做了一个简单的购物车加减按钮部分。
html:
<ul class="counter"> <li id="minus"><input type="button" onclick="minuser()" value="-"/></li> <li id="countnum">0</li> <li id="plus"><input type="button" onclick="adder()" value="+"/></li> </ul>
css(主要是去除按钮默认样式,赋予新样式):
ul,li{margin:0; padding:0; } .counter li{float:left; list-style-type:none; width:30px; height:30px; text-align:center; line-height:30px; border:#999 thin solid; background-color:#fff} .counter li input{font-size:20px; width:100%; height:100%; outline:none; -webkit-appearance:none; background:none; margin:0; padding:0; border: 1px solid transparent; border-radius: 0;} #countnum{ border-left:hidden; border-right:hidden; color:#666}
javascript:
function adder(){ var count=document.getelementbyid("countnum").innerhtml; count=parseint(count)+1; document.getelementbyid("countnum").innerhtml=count; } function minuser(){ var count=document.getelementbyid("countnum").innerhtml; if(count<=0){ count=0; }else{ count=parseint(count)-1; } document.getelementbyid("countnum").innerhtml=count; }
jquery:
function adder(){ var count=$("#countnum").text(); count=parseint(count)+1; $("#countnum").text(count); } function minuser(){ var count=$("#countnum").text(); if(count<=0){ count=0; }else{ count=parseint(count)-1; } $("#countnum").text(count); }
vue版
html:
<p>vue版</p> <ul class="counter"> <li id="minus"><input type="button" v-on:click="minuser" value="-"/></li> <li id="countnum" >{{ count }}</li> <li id="plus"><input type="button" v-on:click="adder" value="+"/></li> </ul>
vue:
new vue({ el: '.counter', data: { count:0 }, methods: { adder: function () { this.count++; }, minuser: function (event) { if(this.count<=0){ this.count=0; }else{ this.count--; } } } });