JS實(shí)現(xiàn)基本的網(wǎng)頁計(jì)算器功能示例
本文實(shí)例講述了JS實(shí)現(xiàn)基本的網(wǎng)頁計(jì)算器功能。分享給大家供大家參考,具體如下:
<html>
<head>
<title>網(wǎng)頁計(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)頁計(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)文本的顯示。但是我們在函數(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測試上述代碼運(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ì)有所幫助。
- js網(wǎng)頁版計(jì)算器的簡單實(shí)現(xiàn)
- 網(wǎng)頁計(jì)算器 一個(gè)JS計(jì)算器
- JSP實(shí)現(xiàn)計(jì)算器功能(網(wǎng)頁版)
- 使用jsp調(diào)用javabean實(shí)現(xiàn)超簡單網(wǎng)頁計(jì)算器示例
- 使用JSP制作一個(gè)超簡單的網(wǎng)頁計(jì)算器的實(shí)例分享
- JavaScript計(jì)算器網(wǎng)頁版實(shí)現(xiàn)代碼分享
- node.js+express制作網(wǎng)頁計(jì)算器
- JavaScript實(shí)現(xiàn)網(wǎng)頁計(jì)算器功能
- javascript實(shí)現(xiàn)簡單的可隨機(jī)變色網(wǎng)頁計(jì)算器示例
- 原生JavaScript實(shí)現(xiàn)網(wǎng)頁版計(jì)算器
相關(guān)文章
一文詳解JSON.parse和JSON.stringify的用法
Json.stringify()和toString()兩者雖然都可以講目標(biāo)值轉(zhuǎn)為字符串,但是還是有本質(zhì)區(qū)別的,下面這篇文章主要給大家介紹了關(guān)于JSON.parse和JSON.stringify用法的相關(guān)資料,需要的朋友可以參考下2023-01-01
深入理解關(guān)于javascript中apply()和call()方法的區(qū)別
下面小編就為大家?guī)硪黄钊肜斫怅P(guān)于javascript中apply()和call()方法的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-04-04
javascript圖片切換綜合實(shí)例(循環(huán)切換、順序切換)
這篇文章主要介紹了javascript圖片切換綜合實(shí)例,包括javascript圖片循環(huán)切換、javascript圖片順序切換,兩張圖片的切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
運(yùn)用Windows XP附帶的Msicuu.exe、Msizap.exe來徹底卸載頑固程序
運(yùn)用Windows XP附帶的Msicuu.exe、Msizap.exe來徹底卸載頑固程序...2007-04-04
JS實(shí)現(xiàn)監(jiān)控微信小程序的原理
這篇文章主要介紹了JS實(shí)現(xiàn)監(jiān)控微信小程序的原理,本文通過實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-06-06
HTML5canvas 繪制一個(gè)圓環(huán)形的進(jìn)度表示實(shí)例
這篇文章主要介紹了HTML5canvas繪制一個(gè)圓環(huán)形的進(jìn)度表示實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12

