JS實現(xiàn)基本的網(wǎng)頁計算器功能示例
本文實例講述了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內(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>
運行效果:

網(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é)構與算法技巧總結(jié)》、《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript事件相關操作與技巧大全》、《JavaScript操作DOM技巧總結(jié)》及《JavaScript字符與字符串操作技巧總結(jié)》
希望本文所述對大家JavaScript程序設計有所幫助。
- js網(wǎng)頁版計算器的簡單實現(xiàn)
- 網(wǎng)頁計算器 一個JS計算器
- JSP實現(xiàn)計算器功能(網(wǎng)頁版)
- 使用jsp調(diào)用javabean實現(xiàn)超簡單網(wǎng)頁計算器示例
- 使用JSP制作一個超簡單的網(wǎng)頁計算器的實例分享
- JavaScript計算器網(wǎng)頁版實現(xiàn)代碼分享
- node.js+express制作網(wǎng)頁計算器
- JavaScript實現(xiàn)網(wǎng)頁計算器功能
- javascript實現(xiàn)簡單的可隨機變色網(wǎng)頁計算器示例
- 原生JavaScript實現(xiàn)網(wǎng)頁版計算器
相關文章
JavaScript的setAttribute兼容性問題解決方法
JavaScript的setAttribute存在兼容性問題,下面與大家分享下具體的解決方法,感興趣的朋友可以參考下2013-11-11
uni-app小程序?qū)崿F(xiàn)微信在線聊天功能(私聊/群聊)
這篇文章主要介紹了uni-app小程序?qū)崿F(xiàn)微信在線聊天(私聊/群聊),今天記錄一下項目核心功能的實現(xiàn)過程。頁面UI以及功能邏輯全部來源于微信,即時聊天業(yè)務的實現(xiàn)使用socket.io,前端使用uni-app開發(fā),后端服務器基于node實現(xiàn),數(shù)據(jù)庫選擇mongoDB,需要的朋友可以參考下2023-02-02
一文詳解requestAnimationFrame請求動畫幀
requestAnimationFrame是一個用于動畫效果的 API,它使用瀏覽器的刷新率來執(zhí)行回調(diào)函數(shù),通常每秒鐘執(zhí)行60次,這篇文章主要給大家介紹了關于requestAnimationFrame請求動畫幀的相關資料,需要的朋友可以參考下2023-12-12

