0
点赞
收藏
分享

微信扫一扫

库存变更器

莞尔小迷糊 2022-02-11 阅读 25
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        span {
            min-width: 50px;
            display: inline-block;
            text-align: center;
            color: red;
        }
    </style>
</head>

<body>
    <p>
        库存的最小值为0
    </p>
    <div>
        库存:
        <button data-plus="-1000">-1000</button>
        <button data-plus="-100">-100</button>
        <button data-plus="-10">-10</button>
        <button data-plus="-1">-</button>
        <span id="spanNumber">10</span>
        <button data-plus="1">+</button>
        <button data-plus="10">+10</button>
        <button data-plus="100">+100</button>
        <button data-plus="1000">+1000</button>
    </div>

    <script>
        var btn = document.querySelectorAll("button")
        for (var i = 0; i < btn.length; i++) {
            btn[i].onclick = function() {
                //获取要变化多少的数值
                var num = parseInt(this.getAttribute("data-plus"))

                // 获取当前库存
                var now = document.getElementById("spanNumber")
                var nowNum = parseInt(now.innerText)


                if ((num + nowNum) > 0) {
                    now.innerText = nowNum + num
                } else {
                    now.innerText = 0
                }

            }
        }
    </script>
</body>

</html>
举报

相关推荐

0 条评论