js實(shí)現(xiàn)網(wǎng)頁(yè)計(jì)算器
如何在利用HTML,css和js的知識(shí)制作一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)計(jì)算器呢?
一個(gè)計(jì)算機(jī)中具備了:
- 計(jì)算機(jī)整體框
- 輸入框
- 輸入按鈕
計(jì)算機(jī)整體框:
/*設(shè)置div樣式*/
#showdiv{
border: solid 1px;
border-radius: 5px;
width: 350px;
height: 400px;
text-align: center;
margin: auto;/*設(shè)置居中*/
margin-top: 50x;
background-color: rgb(214, 219, 190);
}
輸入框:
/*設(shè)置輸入框樣式*/
input[type=text]{
margin-top: 20px;
width: 290px;
height: 40px;
font-size: 20px;
}
輸入按鈕:
/*設(shè)置按鈕樣式*/
input[type=button]{
width: 60px;
height: 60px;
margin-top: 20px;
margin-left: 5px;
margin-right: 5px;
font-size: 30px;
font-weight: bolder;
font-family: "楷書";
}
使用js代碼對(duì)執(zhí)行對(duì)應(yīng)業(yè)務(wù)邏輯操作:
<!--聲明js代碼-->
<script>
function test(btn){
//獲取button按鈕對(duì)象
var number = btn.value;
//執(zhí)行對(duì)應(yīng)的業(yè)務(wù)邏輯
switch (number) {
case "=":
document.getElementById("input").value= eval(document.getElementById("input").value);
break;
case "c":
document.getElementById("input").value="";
break;
default:
//將按鈕的值賦值給input輸入框
document.getElementById("input").value+=number;
break;
}
}
</script>
使用HTML對(duì)計(jì)算機(jī)進(jìn)行排版布局:
<body>
<div id="showdiv">
<input type="text" id="input" readonly="readonly"><br>
<input type="button" value="1" onclick="test(this)">
<input type="button" value="2" onclick="test(this)">
<input type="button" value="3" onclick="test(this)">
<input type="button" value="4" onclick="test(this)"><br>
<input type="button" value="5" onclick="test(this)">
<input type="button" value="6" onclick="test(this)">
<input type="button" value="7" onclick="test(this)">
<input type="button" value="8" onclick="test(this)"><br>
<input type="button" value="9" onclick="test(this)">
<input type="button" value="+" onclick="test(this)">
<input type="button" value="-" onclick="test(this)">
<input type="button" value="*" onclick="test(this)"><br>
<input type="button" value="0" onclick="test(this)">
<input type="button" value="/" onclick="test(this)">
<input type="button" value="c" onclick="test(this)">
<input type="button" value="=" onclick="test(this)">
</div>
</body>
總體代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
/*設(shè)置div樣式*/
#showdiv{
border: solid 1px;
border-radius: 5px;
width: 350px;
height: 400px;
text-align: center;
margin: auto;/*設(shè)置居中*/
margin-top: 50x;
background-color: rgb(214, 219, 190);
}
/*設(shè)置輸入框樣式*/
input[type=text]{
margin-top: 20px;
width: 290px;
height: 40px;
font-size: 20px;
}
/*設(shè)置按鈕樣式*/
input[type=button]{
width: 60px;
height: 60px;
margin-top: 20px;
margin-left: 5px;
margin-right: 5px;
font-size: 30px;
font-weight: bolder;
font-family: "楷書";
}
</style>
<!--聲明js代碼-->
<script>
function test(btn){
//獲取button按鈕對(duì)象
var number = btn.value;
//執(zhí)行對(duì)應(yīng)的業(yè)務(wù)邏輯
switch (number) {
case "=":
document.getElementById("input").value= eval(document.getElementById("input").value);
break;
case "c":
document.getElementById("input").value="";
break;
default:
//將按鈕的值賦值給input輸入框
document.getElementById("input").value+=number;
break;
}
}
</script>
<title>Document</title>
</head>
<body>
<div id="showdiv">
<input type="text" id="input" readonly="readonly"><br>
<input type="button" value="1" onclick="test(this)">
<input type="button" value="2" onclick="test(this)">
<input type="button" value="3" onclick="test(this)">
<input type="button" value="4" onclick="test(this)"><br>
<input type="button" value="5" onclick="test(this)">
<input type="button" value="6" onclick="test(this)">
<input type="button" value="7" onclick="test(this)">
<input type="button" value="8" onclick="test(this)"><br>
<input type="button" value="9" onclick="test(this)">
<input type="button" value="+" onclick="test(this)">
<input type="button" value="-" onclick="test(this)">
<input type="button" value="*" onclick="test(this)"><br>
<input type="button" value="0" onclick="test(this)">
<input type="button" value="/" onclick="test(this)">
<input type="button" value="c" onclick="test(this)">
<input type="button" value="=" onclick="test(this)">
</div>
</body>
</html>
實(shí)現(xiàn)效果:

你一定已經(jīng)學(xué)會(huì)了前端網(wǎng)頁(yè)計(jì)算機(jī)的制作了吧!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- javascript寫的簡(jiǎn)單的計(jì)算器,內(nèi)容很多,方法實(shí)用,推薦
- 簡(jiǎn)易js代碼實(shí)現(xiàn)計(jì)算器操作
- js實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- html+js實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器代碼(加減乘除)
- 用JS寫的簡(jiǎn)單的計(jì)算器實(shí)現(xiàn)代碼
- javascript-簡(jiǎn)單的計(jì)算器實(shí)現(xiàn)步驟分解(附圖)
- js實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器
- 純javascript代碼實(shí)現(xiàn)計(jì)算器功能(三種方法)
- js網(wǎng)頁(yè)版計(jì)算器的簡(jiǎn)單實(shí)現(xiàn)
- 網(wǎng)頁(yè)計(jì)算器 一個(gè)JS計(jì)算器
相關(guān)文章
JS小功能(onmouseover實(shí)現(xiàn)選擇月份)實(shí)例代碼
這篇文章主要介紹了onmouseover實(shí)現(xiàn)選擇月份實(shí)例代碼,有需要的朋友可以參考一下2013-11-11
BootStrap 輪播插件(carousel)支持左右手勢(shì)滑動(dòng)的方法(三種)
這篇文章主要介紹了BootStrap 輪播插件(carousel)支持左右手勢(shì)滑動(dòng)的方法(三種)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
基于JavaScript將表單序列化類型的數(shù)據(jù)轉(zhuǎn)化成對(duì)象的處理(允許對(duì)象中包含對(duì)象)
這篇文章主要介紹了基于JavaScript將表單序列化類型的數(shù)據(jù)轉(zhuǎn)化成對(duì)象的處理(允許對(duì)象中包含對(duì)象) 的相關(guān)資料,需要的朋友可以參考下2015-12-12
獲得Javascript對(duì)象屬性個(gè)數(shù)的示例代碼
這篇文章主要是對(duì)獲得Javascript對(duì)象屬性個(gè)數(shù)的示例代碼進(jìn)行了介紹,需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
JS實(shí)現(xiàn)數(shù)組去重復(fù)值的方法示例
這篇文章主要介紹了JS實(shí)現(xiàn)數(shù)組去重復(fù)值的方法,結(jié)合實(shí)例形式分析了JS通過數(shù)組遍歷、運(yùn)算等方法實(shí)現(xiàn)去重復(fù)值的操作技巧,需要的朋友可以參考下2017-02-02
javascript 刪除dom對(duì)象的事件函數(shù)代碼
本文為《JavaScript高級(jí)程序設(shè)計(jì)》第9章中的跨平臺(tái)事件中的部分內(nèi)容。2010-04-04
JS實(shí)現(xiàn)數(shù)組內(nèi)值累加常見的3個(gè)方法
這篇文章主要給大家介紹了關(guān)于JS實(shí)現(xiàn)數(shù)組內(nèi)值累加常見的3個(gè)方法,文中通過實(shí)例代碼將3個(gè)方法介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用js具有一定參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07

