大神们~html 实现加减按钮

html-css07

大神们~html 实现加减按钮,第1张

//减号添加onclick事件 onclick=subNum()

//显示数字的ID为 num

//加号添加onclick事件 onclick=addNum()

<script>

function subNum(){

document.getElementById("num").value=document.getElementById("num").value-1

}

function addNum(){

document.getElementById("num").value=document.getElementById("num").value+1

}

</script>

<input class="input3 bor-e3 ml-8" id="reduce" type="button" value="-">

<input class="input2 text-c" id="num" type="text" value="1">

<input class="input4 bor-e3" id="add" type="button" value="+">

<!--样式自己写-->   var i = $("#num").val()

       $("#add").click(function () {

           i++

           $("#num").attr("value", i)

       }),

        $("#reduce").click(function (){

            i--

        $("#num").attr("value", i)

         if (i < 0) {

        $("#num").attr("value", 0)

             i = 0

            }

        })

   //jQuery实现的,需要自己去下载jQuery文件

<!DOCTYPE html>  

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />  

<title></title>  

</head>  

<body>  

<table>  

    <tr>  

      <td><input type="button" value="add"     onclick="setOp('+', 'add')"/></td>  

      <td><input type="button" value="miner"  onclick="setOp('-', 'miner')"/></td>  

      <td><input type="button" value="times"  onclick="setOp('*', 'times')"/></td>  

      <td><input type="button" value="divide" onclick="setOp('/', 'divide')"/></td>  

    </tr>  

</table>  

<table id="tb_calc" style="display:none">  

     <tr>  

        <td> <input id="x" type="text" style="width:100px" value="" name="x" /></td>  

        <td> <lable id="op"  name="op"></lable> </td>  

        <td> <input id="y" type="text" style="width:100px" value="" name="y" /> </td>  

        <td> <input id="opTips" type="button" value="" onclick="calc()"/> </td>  

        <td> <lable id="z" name="z"></lable> </td>  

    </tr>  

</table>  

<script type="application/javascript">  

    function setOp(op, opTips)  

    {  

        var tb=document.getElementById("tb_calc")  

        tb.style.display = "none"  

                      

        document.getElementById("x").value = ""   

        document.getElementById("y").value = ""   

        document.getElementById("z").innerText = ""   

        document.getElementById("op").innerText = op  

        document.getElementById("opTips").value = opTips  

          

        tb.style.display = "block"  

    }  

    function calc()  

    {  

        var x  = parseInt(document.getElementById("x").value)   

        var y  = parseInt(document.getElementById("y").value)  

        var op = document.getElementById("op").innerText  

          

        var z = ""  

        switch(op)  

        {  

            case '+':  

                z = x + y  

                break  

            case '-':  

                z = x - y  

                break  

            case '*':   

                z = x * y  

                break  

            case '/':   

                z = x / y  

                break  

            default:  

                z = ''  

        }  

        console.log(x, op, y, '=', z)  

        document.getElementById("z").innerText = z  

    }  

</script>  

</body>  

</html>

实现的步骤,第一步是加4个按钮,如图所示:

第二步需要加两个输入框,两个,一个按钮