zoukankan      html  css  js  c++  java
  • 简易商品加减

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <div>
            <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>
        </div>
        <div>
            <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>
        </div>
        <div>
            <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>
        </div>
    </body>
    </html>
    <script>
    var aAdd = document.querySelectorAll(".add");
    var aSub =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;        //点击数量加1
            var price = this.parentNode.children[0].children[0].innerText.slice(1);//截取单价
            this.nextElementSibling.children[0].innerText = "¥"+(price*num) //总价
        }
    }
    for(var i=0;i<aAdd.length;i++){
        aSub[i].onclick=function(){
            var num=Number(this.nextElementSibling.value);    
            if(num<=0){
                num=1;
                price=0;
            }        
            num--
            this.nextElementSibling.value=num;
            var price=this.parentNode.children[0].children[0].innerText.slice(1);
            this.parentNode.children[4].children[0].innerText="¥"+(price*(num));
        }
    }

    </script>

  • 相关阅读:
    MySQL的char和varchar针对空格的处理
    单KEY业务,数据库水平切分架构实践
    接口测试学习笔记1-接口测试的用例设计
    Robot Framework源码解析(2)
    Robot Framework 源码解析(1)
    Python学习笔记1 -- TypeError: 'str' object is not callable
    OKHttp源码学习同步请求和异步请求(二)
    OKHttp源码学习--HttpURLConnection HttpClient OKHttp Get and post Demo用法对比
    Javapoet源码解析
    Universal-Image-Loader源码解解析---display过程 + 获取bitmap过程
  • 原文地址:https://www.cnblogs.com/ngdty/p/9518585.html
Copyright © 2011-2022 走看看