使用JavaScript 編寫(xiě)簡(jiǎn)單計(jì)算器
本文方法超級(jí)簡(jiǎn)單,思路非常的值得推薦,小伙伴們參考下吧
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript 簡(jiǎn)單計(jì)算器</title>
<script>
//定義全局變量 var one="1";
var two="2";
var three="3";
var four="4";
var five="5";
var six="6";
var seven="7";
var eight="8";
var nine="9";
var zero="0";
var plus="+",minus="-",multiply="*",divide="/",decimal=".";
function enter(obj,string){
obj.expr.value+=string;
}
function compute(obj){//單擊等于"="按鈕調(diào)用的函數(shù); obj.expr.value=eval(obj.expr.value);//用JS得eval()方法計(jì)算name為expr中的公式 }
</script>
</head>
<body>
<form name="calc" id="calc" action="">
<table border="1">
<td colspan="4"><input type="text" name="expr" size="30" action="compute(this.form)" /></td>
<tr>
<td><input type="button" value=" 7 " onclick="enter(this.form,seven)" /></td>
<td><input type="button" value=" 8 " onclick="enter(this.form,eight)" /></td>
<td><input type="button" value=" 9 " onclick="enter(this.form,nine)" /></td>
<td><input type="button" value=" / " onclick="enter(this.form,divide)" /></td>
</tr>
<tr>
<td><input type="button" value=" 4 " onclick="enter(this.form,four)" /></td>
<td><input type="button" value=" 5 " onclick="enter(this.form,five)" /></td>
<td><input type="button" value=" 6 " onclick="enter(this.form,six)" /></td>
<td><input type="button" value=" * " onclick="enter(this.form,multiply)" /></td>
</tr>
<tr>
<td><input type="button" value=" 1 " onclick="enter(this.form,one)" /></td>
<td><input type="button" value=" 2 " onclick="enter(this.form,two)" /></td>
<td><input type="button" value=" 3 " onclick="enter(this.form,three)" /></td>
<td><input type="button" value=" - " onclick="enter(this.form,minus)" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value=" 0 " onclick="enter(this.form,zero)" /></td>
<td><input type="button" value=" . " onclick="enter(this.form,decimal)" /></td>
<td><input type="button" value=" + " onclick="enter(this.form,plus)" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value=" = " onclick="compute(this.form)" /></td>
<td colspan="2"><input type="button" value="AC" onclick="form.reset()"/></td>
</tr>
</table>
</form>
</body>
</html>
是不是超級(jí)NB的代碼,大神級(jí)的!
- javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器效果【推薦】
- 基于javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 純js代碼實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- js實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- 基于JSP實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器的方法
- JS簡(jiǎn)單計(jì)算器實(shí)例
- js的表單操作 簡(jiǎn)單計(jì)算器
- javascript簡(jiǎn)單計(jì)算器 可美化
- js當(dāng)月水電氣簡(jiǎn)單計(jì)算器
- Javascript 實(shí)現(xiàn)簡(jiǎn)單計(jì)算器實(shí)例代碼
相關(guān)文章
Javascript動(dòng)畫(huà)插件lottie-web的使用方法
這篇文章主要介紹了Javascript動(dòng)畫(huà)插件lottie-web的使用方法,包括配合vue-cli使用及在HTML頁(yè)面中使用代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02javaScript中with函數(shù)用法實(shí)例分析
這篇文章主要介紹了javaScript中with函數(shù)用法,實(shí)例分析了javascript中with的功能、定義及相關(guān)使用技巧,需要的朋友可以參考下2015-06-06js通過(guò)keyCode值判斷單擊鍵盤(pán)上某個(gè)鍵,然后觸發(fā)指定的事件方法
下面小編就為大家?guī)?lái)一篇js通過(guò)keyCode值判斷單擊鍵盤(pán)上某個(gè)鍵,然后觸發(fā)指定的事件方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02javascript簡(jiǎn)單拖拽實(shí)現(xiàn)代碼(鼠標(biāo)事件 mousedown mousemove mouseup)
javascript簡(jiǎn)單拖拽,簡(jiǎn)單拖拽實(shí)現(xiàn)2012-05-05JavaScript中使用sencha gridpanel 編輯單元格、改變單元格顏色
ExtJS中的表格功能非常強(qiáng)大,包括了排序、緩存、拖動(dòng)、隱藏某一列、自動(dòng)顯示行號(hào)、列匯總、單元格編輯等實(shí)用功能,通過(guò)本篇文章給大家介紹JavaScript中使用sencha gridpanel 編輯單元、改變單元格顏色,感興趣的朋友一起學(xué)習(xí)2015-11-11基于Bootstrap的Java開(kāi)發(fā)問(wèn)題匯總(Spring MVC)
這篇文章主要為大家匯總了基于Bootstrap的Java開(kāi)發(fā)問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01微信小程序登錄方法之授權(quán)登陸及獲取微信用戶(hù)手機(jī)號(hào)
最近改了一個(gè)公司項(xiàng)目,新增加了一個(gè)獲取用戶(hù)手機(jī)號(hào)功能,里面用到了關(guān)于獲取用戶(hù)信息和用戶(hù)手機(jī)號(hào)的功能,下面這篇文章主要給大家介紹了關(guān)于微信小程序登錄方法之授權(quán)登陸及獲取微信用戶(hù)手機(jī)號(hào)的相關(guān)資料,需要的朋友可以參考下2022-07-07深入了解JavaScript中l(wèi)et/var/function的變量提升
這篇文章主要介紹了深入了解JavaScript中l(wèi)et/var/function的變量提升,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07