最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS實現(xiàn)基本的網(wǎng)頁計算器功能示例

 更新時間:2020年01月16日 11:51:05   作者:qq_42412646  
這篇文章主要介紹了JS實現(xiàn)基本的網(wǎng)頁計算器功能,涉及JavaScript事件響應及數(shù)值運算相關操作技巧,需要的朋友可以參考下

本文實例講述了JS實現(xiàn)基本的網(wǎng)頁計算器功能。分享給大家供大家參考,具體如下:

<html>
    <head>
        <title>網(wǎng)頁計算機</title>
        <meta charset="UTF-8"/>
        <style type="text/css">
            #jsjdiv{
                border: solid 1px black;
                border-radius: 5px;
                width: 200px;
                /*height: 400px;*/
                text-align: center; /*設置div內部居中*/
                margin: auto;    /*設置計算機居中*/
                background-color: darkgrey;
            }
            input[type=text]{
                width: 190px;          /*設置大小*/
                height: 35px;
                margin-top: 10px;   /*設置邊框*/
                margin-bottom: 5px;
            }
            input[type=button]{
                width: 44px;
                height: 44px;
                /*margin-left: 5px;
                margin-right: 5px;*/
                margin-top: 5px;
                margin-bottom: 10px;
                font-size: 25px;  /*設置text的字體大小及深度*/
                font-weight: 600;
            }
        </style>
        <script type="text/javascript">
            function cal(btn){
                var num=btn.value;
                switch (num){   // 利用eval可以把string的內容轉化成代碼,在代碼中輸入可以直接進行計算
                    case "=":
                        document.getElementById("inp").value=eval(document.getElementById("inp").value);
                        break;
                    case "c":
                        document.getElementById("inp").value="";
                        break;
                    default:        //進行輸入數(shù)據(jù)的拼接
                        document.getElementById("inp").value=document.getElementById("inp").value + num;
                        break;
                }
            }
        </script>
    </head>
    <body>
        <div id="jsjdiv">
            <input type="text" name="" id="inp" value="" /><br />
            <input type="button" name="" id="btn" value="1" onclick="cal(this)"/>
            <input type="button" name="" id="" value="2" onclick="cal(this)"/>
            <input type="button" name="" id="" value="3" onclick="cal(this)"/>
            <input type="button" name="" id="" value="4" onclick="cal(this)"/><br />
            <input type="button" name="" id="" value="5" onclick="cal(this)"/>
            <input type="button" name="" id="" value="6" onclick="cal(this)"/>
            <input type="button" name="" id="" value="7" onclick="cal(this)"/>
            <input type="button" name="" id="" value="8" onclick="cal(this)"/><br />
            <input type="button" name="" id="" value="9" onclick="cal(this)"/>
            <input type="button" name="" id="" value="+" onclick="cal(this)"/>
            <input type="button" name="" id="" value="-" onclick="cal(this)"/>
            <input type="button" name="" id="" value="*" onclick="cal(this)"/><br />
            <input type="button" name="" id="" value="0" onclick="cal(this)"/>
            <input type="button" name="" id="" value="/" onclick="cal(this)"/>
            <input type="button" name="" id="" value="c" onclick="cal(this)"/>
            <input type="button" name="" id="" value="=" onclick="cal(this)" />
        </div>
    </body>
</html>

運行效果:

網(wǎng)頁計算機:

利用css進行div的布局設置基本的計算機的基本的框架,

在其內部設置text進行顯示,利用button添加按鈕。

一個主要的點:我們要在按按鈕的時候,把數(shù)據(jù)輸出到text文本上。我們利用了function添加一個函數(shù),在進行按按鈕時,利用onclick,連接到函數(shù),在函數(shù)中實現(xiàn)文本的顯示。但是我們在函數(shù)中只能對某個id進行調用,這樣就表示有多少按鈕就要有多少函數(shù),而且內容相同。所以我們引用了this(當前對象)進行調用。

另一方面,我們要實現(xiàn)計算,我們利用eval()把其中的內容轉化為代碼,就相當于代碼執(zhí)行。所以可以直接進行運算輸出。

當我們輸入“=”和“c"就要進行計算操作,相應的我們利用了switch進行區(qū)分。

感興趣的朋友可以使用在線HTML/CSS/JavaScript前端代碼調試運行工具http://tools.jb51.net/code/WebCodeRun測試上述代碼運行效果。

PS:這里再為大家推薦幾款計算工具供大家進一步參考借鑒:

在線一元函數(shù)(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科學計算器在線使用_高級計算器在線計算:
http://tools.jb51.net/jisuanqi/jsqkexue

在線計算器_標準計算器:
http://tools.jb51.net/jisuanqi/jsq

更多關于JavaScript相關內容還可查看本站專題:《JavaScript數(shù)學運算用法總結》、《JavaScript數(shù)據(jù)結構與算法技巧總結》、《JavaScript數(shù)組操作技巧總結》、《JavaScript事件相關操作與技巧大全》、《JavaScript操作DOM技巧總結》及《JavaScript字符與字符串操作技巧總結

希望本文所述對大家JavaScript程序設計有所幫助。

相關文章

最新評論

济南市| 偃师市| 鄂伦春自治旗| 贵阳市| 闵行区| 梁河县| 嘉兴市| 遂宁市| 隆安县| 呼和浩特市| 兴海县| 喀喇沁旗| 扬州市| 兖州市| 景泰县| 肥东县| 保亭| 新干县| 怀来县| 嘉兴市| 商都县| 林口县| 松桃| 博客| 黔东| 汕尾市| 五华县| 天镇县| 乃东县| 浦江县| 太仓市| 偏关县| 灵山县| 孟津县| 江川县| 河西区| 柯坪县| 彭山县| 新兴县| 楚雄市| 苏尼特左旗|