javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼
來(lái)源:易賢網(wǎng) 閱讀:1376 次 日期:2016-06-17 15:59:09
溫馨提示:易賢網(wǎng)小編為您整理了“javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼”,方便廣大網(wǎng)友查閱!

下面小編就為大家?guī)?lái)一篇javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。

今天閑來(lái)無(wú)聊,想寫(xiě)點(diǎn)什么,突然想到用javascript寫(xiě)一個(gè)計(jì)算器。程序還存在很多的Bug,先在這里記錄一下,以后慢慢更正。

名單

代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼_腳本之家</title>

<style type="text/css">

input{

width:30px;

height:20px;

text-align:center;

}

#tbCalculator td

{

text-align:center;

vertical-align:middle;

}

</style>

<script type="text/javascript">

var result; //保存點(diǎn)擊運(yùn)算符之前輸入框中的數(shù)值

var operator; //保存運(yùn)算符

var isPressEqualsKey = false; //記錄是否按下”=“鍵

//數(shù)字鍵事件

function connectionDigital(control)

{

var txt = document.getElementById('txtScream');

if(isPressEqualsKey)

txt.value = ""; //已進(jìn)行過(guò)計(jì)算,則清空數(shù)值輸入框重新開(kāi)始

isPressEqualsKey = false;

}

//數(shù)值輸入已經(jīng)存在小數(shù)點(diǎn),則不允許再輸入小數(shù)點(diǎn)

if(txt.value.indexOf('.') > -1 && control.value == '.')

return false;

txt.value += control.value; //將控件值賦給數(shù)值輸入框中

}

//退格鍵事件

function backspace()

{

var txt = document.getElementById('txtScream');

txt.value = txt.value.substring(0,txt.value.length - 1);

}

//ce鍵事件:清空數(shù)字輸入框

function clearAll()

{

document.getElementById('txtScream').value = "";

result = "";

operator = "";

}

// +、-、*、/ 事件

function calculation(control)

{

//將運(yùn)算符保存入全局變量中

operator = control.value; 

var txt = document.getElementById('txtScream');

if(txt.value == "")return false; //數(shù)值輸入框中沒(méi)有數(shù)字,則不能輸入運(yùn)算符

//將數(shù)值輸入框中的值保存到計(jì)算表達(dá)式中

result = txt.value; 

//清空輸入框,以待輸入操作值

txt.value = ""; 

}

//計(jì)算結(jié)果

function getResult()

{

var opValue;

//計(jì)算表達(dá)式中存在運(yùn)算符

var sourseValue = parseFloat(result);

var txt = document.getElementById('txtScream');

if(operator == '*')

opValue = sourseValue * parseFloat(txt.value);

else if(operator == '/')

opValue = sourseValue / parseFloat(txt.value);

else if(operator == '+')

opValue = sourseValue + parseFloat(txt.value);

else if(operator == '-')

opValue = sourseValue - parseFloat(txt.value);

txt.value = opValue;

isPressEqualsKey = true;

result = "";

opValue = "";

}

</script>

</head>

<body>

<table id="tbCalculator" width="200" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#0066FF">

<tr>

<td height="30" colspan="4" align="center">

<input type="text" name="txtScream" id="txtScream" style="width:180px; border-style:none; text-align:right;" readonly="readonly" /> </td>

</tr>

<tr>

<td height="30" colspan="2">

<input type="button" name="btnCE" id="btnCE" value="C E" style="width:80px;" align="right"; onclick="clearAll();" /></td>

<td height="30" colspan="2">

<input type="button" name="btn10" id="btn10" value="Backspace" style="width:80px;" align="right"; onclick="backspace();" /></td>

</tr>

<tr>

<td height="30"><input type="button" name="btn7" id="btn7" value="7" onclick="connectionDigital(this);" /></td>

<td><input type="button" name="btn8" id="btn8" value="8" onclick="connectionDigital(this);"/></td>

<td><input type="button" name="btn9" id="btn9" value="9" onclick="connectionDigital(this);" /></td>

<td><input type="button" name="btn6" id="btn6" value="/" onclick="calculation(this);" /></td>

</tr>

<tr>

<td height="30">

<input type="button" name="btn4" id="btn4" value="4" onclick="connectionDigital(this);"/></td>

<td><input type="button" name="btn5" id="btn5" value="5" onclick="connectionDigital(this);"/></td>

<td><input type="button" name="btn6" id="btn6" value="6" onclick="connectionDigital(this);"/></td>

<td><input type="button" name="btn13" id="btn13" value="*" onclick="calculation(this);" /></td>

</tr>

<tr>

<td height="30">

<input type="button" name="btn1" id="btn1" value="1" onclick="connectionDigital(this);"/></td>

<td><input type="button" name="btn2" id="btn2" value="2" onclick="connectionDigital(this);"/></td>

<td><input type="button" name="btn3" id="btn3" value="3" onclick="connectionDigital(this);"/></td>

<td><input type="button" name="btn18" id="btn18" value="-" onclick="calculation(this);" /></td>

</tr>

<tr>

<td height="30"><input type="button" name="btn0" id="btn0" value="0" onclick="connectionDigital(this);"/></td>

<td><input type="button" name="btndot" id="btndot" value="." onclick="connectionDigital(this);" /></td>

<td><input name="btn22" type="button" id="btn22" value="=" onclick="getResult();" /></td>

<td><input type="button" name="btn23" id="btn23" value="+" onclick="calculation(this);" /></td>

</tr>

</table>

</body>

</html>

以上這篇javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)