<!DOCTYPE?html>
<html?lang="en">
<head>
??<meta?charset="UTF-8">
??<title>1</title>
??<script?type="text/javascript">
?var?cclater?=?{
????a:?parseInt(document.getElementById("txt1").value),
?b:?parseInt(document.getElementById("txt2").value),
?sign:?parseInt(document.getElementById("select").value),
?operation:?function?()?{
??????switch?(this.sign)?{
????????case?"+":
??????????return?this.a?+?this.b;
?case??"-":
??????????return?this.a?-?this.b;
?case??"*":
??????????return?this.a?*?this.b;
?case??"/":
??????????return?this.a?/?this.b;
?}
????},
?setvalue:?function?()?{
??????document.getElementById("fruit").value?=?this.operation();
?}
??};
?//獲取第一個輸入框的值
??//獲取第二個輸入框的值
??//獲取選擇框的值
??//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
??//設置結果輸入框的值
?</script>
</head>
<body>
<input?type='text'?id='txt1'/>
<select?id='select'>
??<option?value='+'>+</option>
??<option?value="-">-</option>
??<option?value="*">*</option>
??<option?value="/">/</option>
</select>
<input?type='text'?id='txt2'/>
<input?type='button'?value='?=?'?onclick="cclater.setvalue()"/>?<!--通過?=?按鈕來調用創建的函數,得到結果-->
<input?type='text'?id='fruit'/>
</body>
</html>
2016-11-18
<!DOCTYPE?html> <html> <head> ??<meta?charset="UTF-8"> ??<title>1</title> </head> <body> <input?type='text'?id='txt1'?value="2"/> <select?id='select'> ??<option?value='+'>+</option> ??<option?value="-">-</option> ??<option?value="*">*</option> ??<option?value="/">/</option> </select> <input?type='text'?id='txt2'?value="2"?/> <input?type='button'?value='?=?'?onclick="cclater.setvalue()"/> <input?type='text'?id='fruit'/> ??<script?type="text/javascript"> ?var?cclater?=?{ ? ????a:?parseInt(document.getElementById("txt1").value), ?b:?parseInt(document.getElementById("txt2").value), ?sign:?document.getElementById("select").value, ? ?operation:?function?()?{ ??????switch?(this.sign)?{ ????????case?"+": ??????????return?this.a?+?this.b; ?case??"-": ??????????return?this.a?-?this.b; ?case??"*": ??????????return?this.a?*?this.b; ?case??"/": ??????????return?this.a?/?this.b; ?????default: ?????return?1; ?} ????}, ?update?:function?()?{ ????this.a=?parseInt(document.getElementById("txt1").value); ?this.b=?parseInt(document.getElementById("txt2").value); ?this.sign=?document.getElementById("select").value;?? ?}, ?setvalue:?function?()?{ ??this.update(); ??????document.getElementById("fruit").value?=?this.operation(); ?} ??};? ? ?//獲取第一個輸入框的值 ??//獲取第二個輸入框的值 ??//獲取選擇框的值 ??//獲取通過下拉框來選擇的值來改變加減乘除的運算法則 ??//設置結果輸入框的值 ?</script> </body> </html>