亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js實(shí)現(xiàn)網(wǎng)頁計(jì)算器

 更新時(shí)間:2021年05月18日 09:57:07   作者:一號程序猿  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)網(wǎng)頁計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

如何在利用HTML,css和js的知識制作一個(gè)簡單的網(wǎng)頁計(jì)算器呢?

一個(gè)計(jì)算機(jī)中具備了:

  • 計(jì)算機(jī)整體框
  • 輸入框
  • 輸入按鈕

計(jì)算機(jī)整體框:

/*設(shè)置div樣式*/
 #showdiv{
   border: solid 1px;
   border-radius: 5px;
   width: 350px;
   height: 400px;
   text-align: center;
   margin: auto;/*設(shè)置居中*/
   margin-top: 50x;
   background-color: rgb(214, 219, 190);    
}

輸入框:

/*設(shè)置輸入框樣式*/
  input[type=text]{
      margin-top: 20px;
      width: 290px;
      height: 40px;
      font-size: 20px;

}

輸入按鈕:

/*設(shè)置按鈕樣式*/
   input[type=button]{
      width: 60px;
      height: 60px;
      margin-top: 20px;
      margin-left: 5px;
      margin-right: 5px;
      font-size: 30px;
      font-weight: bolder;
      font-family: "楷書";
}

使用js代碼對執(zhí)行對應(yīng)業(yè)務(wù)邏輯操作:

<!--聲明js代碼-->
        <script>
            function test(btn){
                //獲取button按鈕對象
                var number = btn.value;
                //執(zhí)行對應(yīng)的業(yè)務(wù)邏輯
                switch (number) {
                    case "=":
                        document.getElementById("input").value= eval(document.getElementById("input").value);
                        break;
                    case "c":
                        document.getElementById("input").value="";
                        break;
                    default:
                        //將按鈕的值賦值給input輸入框
                        document.getElementById("input").value+=number;
                        break;
                }
                
            }
</script>

使用HTML對計(jì)算機(jī)進(jìn)行排版布局:

<body>
    <div id="showdiv">
        <input type="text"  id="input" readonly="readonly"><br>
        <input type="button" value="1" onclick="test(this)">
        <input type="button" value="2" onclick="test(this)">
        <input type="button" value="3" onclick="test(this)">
        <input type="button" value="4" onclick="test(this)"><br>
        <input type="button" value="5" onclick="test(this)">
        <input type="button" value="6" onclick="test(this)">
        <input type="button" value="7" onclick="test(this)">
        <input type="button" value="8" onclick="test(this)"><br>
        <input type="button" value="9" onclick="test(this)">
        <input type="button" value="+" onclick="test(this)">
        <input type="button" value="-" onclick="test(this)">
        <input type="button" value="*" onclick="test(this)"><br>
        <input type="button" value="0" onclick="test(this)">
        <input type="button" value="/" onclick="test(this)">
        <input type="button" value="c" onclick="test(this)">
        <input type="button" value="=" onclick="test(this)">
      


    </div>
</body>

總體代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        /*設(shè)置div樣式*/
        #showdiv{
            border: solid 1px;
            border-radius: 5px;
            width: 350px;
            height: 400px;
            text-align: center;
            margin: auto;/*設(shè)置居中*/
            margin-top: 50x;
            background-color: rgb(214, 219, 190);    
         }
        /*設(shè)置輸入框樣式*/
        input[type=text]{
            margin-top: 20px;
            width: 290px;
            height: 40px;
            font-size: 20px;

        }
        /*設(shè)置按鈕樣式*/
        input[type=button]{
            width: 60px;
            height: 60px;
            margin-top: 20px;
            margin-left: 5px;
            margin-right: 5px;
            font-size: 30px;
            font-weight: bolder;
            font-family: "楷書";
        }
        </style>
        <!--聲明js代碼-->
        <script>
            function test(btn){
                //獲取button按鈕對象
                var number = btn.value;
                //執(zhí)行對應(yīng)的業(yè)務(wù)邏輯
                switch (number) {
                    case "=":
                        document.getElementById("input").value= eval(document.getElementById("input").value);
                        break;
                    case "c":
                        document.getElementById("input").value="";
                        break;
                    default:
                        //將按鈕的值賦值給input輸入框
                        document.getElementById("input").value+=number;
                        break;
                }
                
            }
        </script>
        
        
    <title>Document</title>
</head>
<body>
    <div id="showdiv">
        <input type="text"  id="input" readonly="readonly"><br>
        <input type="button" value="1" onclick="test(this)">
        <input type="button" value="2" onclick="test(this)">
        <input type="button" value="3" onclick="test(this)">
        <input type="button" value="4" onclick="test(this)"><br>
        <input type="button" value="5" onclick="test(this)">
        <input type="button" value="6" onclick="test(this)">
        <input type="button" value="7" onclick="test(this)">
        <input type="button" value="8" onclick="test(this)"><br>
        <input type="button" value="9" onclick="test(this)">
        <input type="button" value="+" onclick="test(this)">
        <input type="button" value="-" onclick="test(this)">
        <input type="button" value="*" onclick="test(this)"><br>
        <input type="button" value="0" onclick="test(this)">
        <input type="button" value="/" onclick="test(this)">
        <input type="button" value="c" onclick="test(this)">
        <input type="button" value="=" onclick="test(this)">
      


    </div>
</body>
</html>

實(shí)現(xiàn)效果:

你一定已經(jīng)學(xué)會了前端網(wǎng)頁計(jì)算機(jī)的制作了吧!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論