JS之物件導向

來源:互聯網
上載者:User

標籤:pac   type   script   sum   var   htm   i++   cli   length   

用物件導向方法做一個計算機:

html代碼:

<style type="text/css">
table{
margin: 0 auto;
text-align: center;
}
input{
width: 100px;
height: 80px;
font-size: 60px;
}
#sum{
width: 480px;
}

</style>

<body>
<table width="500" height="600" border="1" cellpadding="0" cellspacing="2">
<tr >
<td colspan="4">
<input type="text" id="sum"/>
</td>

</tr>
<tr >
<td><input type="button" value="1" id="num1" name="num"/></td>
<td><input type="button" value="2" id="num2" name="num"/></td>
<td><input type="button" value="3" id="num3" name="num"/></td>
<td><input type="button" value="+" id="fuhao1" name="fuhao"/></td>


</tr>
<tr >
<td><input type="button" value="4" id="num4" name="num"/></td>
<td><input type="button" value="5" id="num5" name="num"/></td>
<td><input type="button" value="6" id="num6" name="num"/></td>
<td><input type="button" value="-" id="fuhao2" name="fuhao"/></td>

</tr>
<tr >
<td><input type="button" value="7" id="num7" name="num"/></td>
<td><input type="button" value="8" id="num8" name="num"/></td>
<td><input type="button" value="9" id="num9" name="num"/></td>
<td><input type="button" value="*" id="fuhao3" name="fuhao"/></td>

</tr>
<tr height="100">

<td><input type="button" value="0" id="num0" name="num"/></td>
<td><input type="button" value="." id="num." name="num"/></td>
<td><input type="button" value="=" id="dengyu" name="dengyu"/></td>
<td><input type="button" value="/" id="fuhao4" name="fuhao"/></td>

</tr>
</table>
</body>

<script src="js/counter.js" type="text/javascript" charset="utf-8"></script>

script代碼:

var counters = {
num1: 0,
fuhao: "",
sum: document.getElementById("sum"),
getnum: function(num) {
this.sum.value += num;
},
getfuhao: function(fuhao) {
this.num1 = this.sum.value;
this.fuhao = fuhao;
this.sum.value = "";

},
getvalue: function() {
this.sum.value=eval(this.num1+this.fuhao+this.sum.value);

}
}

//點擊數字,顯示數字
var objnum = document.getElementsByName(‘num‘);
for(var i = 0; i < objnum.length; i++) {
objnum.item(i).onclick = function() {
//alert(this.value);
counters.getnum(this.value);
}
}
//點擊符號,顯示符號
var objfuhao = document.getElementsByName(‘fuhao‘);
for(var j = 0; j < objfuhao.length; j++) {
objfuhao.item(j).onclick = function() {
counters.getfuhao(this.value);
//alert("aa");
}
}
//點擊等號,顯示結果
var objvalue = document.getElementById(‘dengyu‘);
objvalue.onclick=function(){
counters.getvalue();
//alert(‘aa‘);
}

 

JS之物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.