欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)基本的網(wǎng)頁(yè)計(jì)算器功能示例

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

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

<html>
    <head>
        <title>網(wǎng)頁(yè)計(jì)算機(jī)</title>
        <meta charset="UTF-8"/>
        <style type="text/css">
            #jsjdiv{
                border: solid 1px black;
                border-radius: 5px;
                width: 200px;
                /*height: 400px;*/
                text-align: center; /*設(shè)置div內(nèi)部居中*/
                margin: auto;    /*設(shè)置計(jì)算機(jī)居中*/
                background-color: darkgrey;
            }
            input[type=text]{
                width: 190px;          /*設(shè)置大小*/
                height: 35px;
                margin-top: 10px;   /*設(shè)置邊框*/
                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;  /*設(shè)置text的字體大小及深度*/
                font-weight: 600;
            }
        </style>
        <script type="text/javascript">
            function cal(btn){
                var num=btn.value;
                switch (num){   // 利用eval可以把string的內(nèi)容轉(zhuǎn)化成代碼,在代碼中輸入可以直接進(jìn)行計(jì)算
                    case "=":
                        document.getElementById("inp").value=eval(document.getElementById("inp").value);
                        break;
                    case "c":
                        document.getElementById("inp").value="";
                        break;
                    default:        //進(jìn)行輸入數(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>

運(yùn)行效果:

網(wǎng)頁(yè)計(jì)算機(jī):

利用css進(jìn)行div的布局設(shè)置基本的計(jì)算機(jī)的基本的框架,

在其內(nèi)部設(shè)置text進(jìn)行顯示,利用button添加按鈕。

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

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

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

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

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

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

科學(xué)計(jì)算器在線使用_高級(jí)計(jì)算器在線計(jì)算:
http://tools.jb51.net/jisuanqi/jsqkexue

在線計(jì)算器_標(biāo)準(zhǔn)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsq

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

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論