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

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

創(chuàng)新互聯(lián)網(wǎng)站建設公司一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務為基礎,以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個客戶多一個朋友!專注中小微企業(yè)官網(wǎng)定制,成都網(wǎng)站制作、成都網(wǎng)站設計、外貿(mào)營銷網(wǎng)站建設,塑造企業(yè)網(wǎng)絡形象打造互聯(lián)網(wǎng)企業(yè)效應。

<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內(nèi)部居中*/
                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的內(nèi)容轉(zhuǎn)化成代碼,在代碼中輸入可以直接進行計算
                    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>

運行效果:

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

網(wǎng)頁計算機:

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

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

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

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

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

感興趣的朋友可以使用在線HTML/CSS/JavaScript前端代碼調(diào)試運行工具: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相關內(nèi)容還可查看本站專題:《JavaScript數(shù)學運算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript事件相關操作與技巧大全》、《JavaScript操作DOM技巧總結(jié)》及《JavaScript字符與字符串操作技巧總結(jié)》

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

網(wǎng)站標題:JS實現(xiàn)基本的網(wǎng)頁計算器功能示例
文章轉(zhuǎn)載:http://bm7419.com/article4/igidie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、外貿(mào)建站、域名注冊、標簽優(yōu)化服務器托管、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設