zoukankan      html  css  js  c++  java
  • 价格计算

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    </head>
    <script>
    window.onload = function()
    {
    var oUl = document.getElementById('list');
    var oTotalPrice = document.getElementById('totalPrice');
    var oExpensive = document.getElementById('expensive');
    var aLi = oUl.getElementsByTagName('li');
    var aSpan = oUl.getElementsByTagName('span');
    var aEm= oUl.getElementsByTagName('em');
    var aFont = oUl.getElementsByTagName('font');

    function fnComputer(aLi){
    //var aLi = oUl.getElementsByTagName('li')[0];

    var aInput = aLi.getElementsByTagName('input');
    var aSpan = aLi.getElementsByTagName('span')[0];
    var aEm= aLi.getElementsByTagName('em')[0];
    var aFont = aLi.getElementsByTagName('font')[0];

    aInput[1].onclick = function(){
    var nProNum = parseInt(aSpan.innerHTML)+1;
    aSpan.innerHTML = nProNum;
    var nProPrice = parseFloat(aEm.innerHTML);
    var totalPrice = nProPrice*nProNum;
    aFont.innerHTML = totalPrice+ '元';
    sum();
    }

    aInput[0].onclick = function(){
    var nProNum = parseInt(aSpan.innerHTML)-1;
    if(nProNum<0){
    nProNum=0;
    }
    aSpan.innerHTML = nProNum;
    var nProPrice = parseFloat(aEm.innerHTML);
    var totalPrice = nProPrice*nProNum;
    aFont.innerHTML = totalPrice+ '元';
    sum();

    }
    }

    for(var i=0;i<aLi.length;i++){
    fnComputer(aLi[i]);
    }

    function sum(){
    var totalPrice = 0;
    var nProNum = 0;
    var nExpenPrice = 0;

    for(var i=0;i<aFont.length;i++){
    totalPrice += parseFloat(aFont[i].innerHTML);
    nProNum += parseInt(aSpan[i].innerHTML);

    if(aSpan[i].innerHTML != 0){

    if(parseFloat(nExpenPrice)<parseFloat(aEm[i].innerHTML)){
    nExpenPrice = aEm[i].innerHTML;
    }

    }
    }

    oTotalPrice.innerHTML = '商品总价为:'+totalPrice+'$';
    oExpensive.innerHTML = '最贵商品的单价:'+nExpenPrice+', 共'+nProNum+'件';
    }
    };
    </script>
    <body>
    <ul id="list">
    <li><input type="button" value="-"> <span>0</span> <input type="button" value="+" > <em>20$</em> <font>0元</font></li>
    <li><input type="button" value="-"> <span>0</span> <input type="button" value="+" > <em>11$</em> <font>0元</font></li>
    <li><input type="button" value="-"> <span>0</span> <input type="button" value="+" > <em>100$</em> <font>0元</font></li>
    <li><input type="button" value="-"> <span>0</span> <input type="button" value="+" > <em>320$</em> <font>0元</font></li>
    </ul>
    <p id="totalPrice">商品总价为:0$</p>
    <p id="expensive">最贵商品的单价:0$ , 共0件</p>
    </body>
    </html>

  • 相关阅读:
    CSS3 动画--- CSS3 animation
    CSS3 3D变形 transform---rotateX(), rotateY(), rotateZ(), 透视(perspective)
    CSS3 2D变形 transform---移动 translate(x, y), 缩放 scale(x, y), 旋转 rotate(deg), transform-origin, 倾斜 skew(deg, deg)
    CSS3 过渡---transition
    情人节用Python智能聊天机器人的实现|制作一个虚拟恋人
    Python基础知识详解 从入门到精通(七)类与对象
    Python核心编程:8个实践性建议
    给Python初学者的一些编程技巧
    30分钟编写一个抓取 Unsplash 图片的 Python爬虫
    Python反编译调用有道翻译(附完整代码)
  • 原文地址:https://www.cnblogs.com/gyc51/p/8376851.html
Copyright © 2011-2022 走看看