js的表單操作 簡(jiǎn)單計(jì)算器
更新時(shí)間:2011年12月29日 00:09:48 作者:
javascript寫的簡(jiǎn)單的加減乘除計(jì)算器,里面涉及到一些方法還是很實(shí)用的哦,新手不要錯(cuò)過(guò)
代碼:
<!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>寫給新手:js簡(jiǎn)單計(jì)算器</title>
<style type="text/css">
body{
font-size:12px;
color:#333;
}
#jsq input{/*輸入框樣式*/
border:#ccc 1px solid;
border-right:#e2e2e2 1px solid;
border-bottom:#e2e2e2 1px solid;
height:18px;
line-height:18px;
padding:3px;
}
#jsq span{
color:#999
}
#jsq input.btn{/*按鈕樣式*/
border:#e6e6e6 1px solid;
background-color:#e2e2e2;
width:30px;
height:24px;
text-align:center;
line-height:16px;
cursor:pointer;
margin:0 3px;
color:#999;
}
#jsq input.btn:hover{/*按鈕懸浮樣式*/
border:#e2e2e2 1px solid;
background-color:#f2f2f2;
color:#333;
}
</style>
<script type="text/javascript">
function imyeah(type){ //計(jì)算函數(shù)
var result=0;
num1 = Number(document.jisuanqi.num1.value); //Number()可以吧字符串強(qiáng)制轉(zhuǎn)換成數(shù)字,例如“123abc”會(huì)轉(zhuǎn)換成“123”
num2 = Number(document.jisuanqi.num2.value);
if(num1=="" || num2==""){return false;} //如果沒輸入計(jì)算數(shù)則不執(zhí)行
switch(type){ //判斷要執(zhí)行的計(jì)算符號(hào)
case 0:result=num1+num2;break; //計(jì)算“+”
case 1:result=num1-num2;break; //計(jì)算“-”
case 2:result=num1*num2;break;
case 3:result=num1/num2;break;
case 4:result=num1%num2;break;
}
document.jisuanqi.jieguo.value=result; //顯示計(jì)算結(jié)果
}
</script>
</head>
<body>
<form name="jisuanqi" id="jsq" action="" method="get" />
<p> 第一個(gè)數(shù):
<input type="text" size="10" name="num1" value="" />
</p>
<p> 第二個(gè)數(shù):
<input type="text" size="10" name="num2" value="" />
</p>
<p> 計(jì)算結(jié)果:
<input type="text" size="10" name="jieguo" onClick="imyeah(0)" value="+" onfocus="this.select()" /> <span>左鍵"+",右鍵"選中復(fù)制"</span>
</p>
<p>
<input type="button" class="btn" value="–" onClick="imyeah(1)"/> <!--定義按鈕-->
<input type="button" class="btn" value="×" onClick="imyeah(2)"/ >
<input type="button" class="btn" value="÷" onClick="imyeah(3)"/>
<input type="button" class="btn" value="%" onClick="imyeah(4)"/>
</p>
</body>
</html>
運(yùn)行效果:
復(fù)制代碼 代碼如下:
<!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>寫給新手:js簡(jiǎn)單計(jì)算器</title>
<style type="text/css">
body{
font-size:12px;
color:#333;
}
#jsq input{/*輸入框樣式*/
border:#ccc 1px solid;
border-right:#e2e2e2 1px solid;
border-bottom:#e2e2e2 1px solid;
height:18px;
line-height:18px;
padding:3px;
}
#jsq span{
color:#999
}
#jsq input.btn{/*按鈕樣式*/
border:#e6e6e6 1px solid;
background-color:#e2e2e2;
width:30px;
height:24px;
text-align:center;
line-height:16px;
cursor:pointer;
margin:0 3px;
color:#999;
}
#jsq input.btn:hover{/*按鈕懸浮樣式*/
border:#e2e2e2 1px solid;
background-color:#f2f2f2;
color:#333;
}
</style>
<script type="text/javascript">
function imyeah(type){ //計(jì)算函數(shù)
var result=0;
num1 = Number(document.jisuanqi.num1.value); //Number()可以吧字符串強(qiáng)制轉(zhuǎn)換成數(shù)字,例如“123abc”會(huì)轉(zhuǎn)換成“123”
num2 = Number(document.jisuanqi.num2.value);
if(num1=="" || num2==""){return false;} //如果沒輸入計(jì)算數(shù)則不執(zhí)行
switch(type){ //判斷要執(zhí)行的計(jì)算符號(hào)
case 0:result=num1+num2;break; //計(jì)算“+”
case 1:result=num1-num2;break; //計(jì)算“-”
case 2:result=num1*num2;break;
case 3:result=num1/num2;break;
case 4:result=num1%num2;break;
}
document.jisuanqi.jieguo.value=result; //顯示計(jì)算結(jié)果
}
</script>
</head>
<body>
<form name="jisuanqi" id="jsq" action="" method="get" />
<p> 第一個(gè)數(shù):
<input type="text" size="10" name="num1" value="" />
</p>
<p> 第二個(gè)數(shù):
<input type="text" size="10" name="num2" value="" />
</p>
<p> 計(jì)算結(jié)果:
<input type="text" size="10" name="jieguo" onClick="imyeah(0)" value="+" onfocus="this.select()" /> <span>左鍵"+",右鍵"選中復(fù)制"</span>
</p>
<p>
<input type="button" class="btn" value="–" onClick="imyeah(1)"/> <!--定義按鈕-->
<input type="button" class="btn" value="×" onClick="imyeah(2)"/ >
<input type="button" class="btn" value="÷" onClick="imyeah(3)"/>
<input type="button" class="btn" value="%" onClick="imyeah(4)"/>
</p>
</body>
</html>
運(yùn)行效果:
您可能感興趣的文章:
- 簡(jiǎn)易js代碼實(shí)現(xiàn)計(jì)算器操作
- javascript-簡(jiǎn)單的計(jì)算器實(shí)現(xiàn)步驟分解(附圖)
- js網(wǎng)頁(yè)版計(jì)算器的簡(jiǎn)單實(shí)現(xiàn)
- 純javascript代碼實(shí)現(xiàn)計(jì)算器功能(三種方法)
- 使用jsp調(diào)用javabean實(shí)現(xiàn)超簡(jiǎn)單網(wǎng)頁(yè)計(jì)算器示例
- js實(shí)現(xiàn)模擬計(jì)算器退格鍵刪除文字效果的方法
- html+js實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器代碼(加減乘除)
- JSP實(shí)現(xiàn)計(jì)算器功能(網(wǎng)頁(yè)版)
- JavaScript計(jì)算器網(wǎng)頁(yè)版實(shí)現(xiàn)代碼分享
- js當(dāng)月水電氣簡(jiǎn)單計(jì)算器
- javascript計(jì)算當(dāng)月剩余天數(shù)(天數(shù)計(jì)算器)示例代碼
- JS實(shí)現(xiàn)的加減乘除四則運(yùn)算計(jì)算器示例
相關(guān)文章
js扁平數(shù)組和樹結(jié)構(gòu)相互轉(zhuǎn)換處理方法
這篇文章主要給大家介紹了關(guān)于js扁平數(shù)組和樹結(jié)構(gòu)相互轉(zhuǎn)換處理方法的相關(guān)資料,之前面試有遇到過(guò)這個(gè)問題,面試官問如何把一個(gè)數(shù)組數(shù)據(jù)扁平,然后轉(zhuǎn)化為Tree結(jié)構(gòu)數(shù)據(jù),工作中剛好也用到了,所以總結(jié)下,需要的朋友可以參考下2023-07-07
JavaScript實(shí)現(xiàn)前端飛機(jī)大戰(zhàn)小游戲
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)前端飛機(jī)大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
簡(jiǎn)單實(shí)現(xiàn)js倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了js倒計(jì)時(shí)效果的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
js定時(shí)器出現(xiàn)第一次延遲的原因及解決方法
在本篇文章里小編給大家整理的是一篇關(guān)于js定時(shí)器出現(xiàn)第一次延遲的原因及解決方法,對(duì)此有需要的朋友們可以學(xué)習(xí)下。2021-01-01
ES6 javascript中Class類繼承用法實(shí)例詳解
這篇文章主要介紹了ES6 javascript中Class類繼承用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了ES6繼承的基本用法、相關(guān)屬性、方法與使用技巧,需要的朋友可以參考下2017-10-10
JavaScript 如何實(shí)現(xiàn)同源通信
在日常工作中,你可能會(huì)遇到同源頁(yè)面間通信的場(chǎng)景。針對(duì)這種場(chǎng)景,我們可以使用 localStorage 和 storage 事件來(lái)解決同源頁(yè)面間通信的問題。除此之外,我們還可以使用 Broadcast Channel API 來(lái)解決該問題。接下來(lái),將帶大家一起來(lái)認(rèn)識(shí)一下 Broadcast Channel API。2021-05-05

