媒介:初学js,先生请求做一个浅易盘算器,现在只完成了可以盘算,许多束缚没有有做,待完美。仅作为本身的进修纪录。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>浅易盘算器</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<script src="js/calculator.js" type="text/javascript"></script>
</head>
<body>
<!--盘算器-->
<div class="calculator" id="calculator">
<div class="cal_topBar">.....</div>
<div class="cal_header" >
<!--设置窗口,运用onfocus="this.blur();"防止键盘输入-->
<input type="text" value="0" class="res_box" onFocus="this.blur()" id="result"/>
</div>
<!--设置按钮-->
<div class="cal_body" id="calcu-btn">
<div style="height:1px;"></div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href="#" class="w_1 operator" onClick="command(NaN)">mc</a></td>
<td><a href="#" class="w_1 operator" onClick="command(NaN)">m+</a></td>
<td><a href="#" class="w_1 operator" onClick="command(NaN)">m-</a></td>
<td><a href="#" class="w_1 operator" onClick="command(NaN)">mr</a></td>
</tr>
<tr>
<td><a href="#" class="w_1" onClick="command('C')">C</a></td>
<td><a href="#" class="w_1" onClick="command(1)">±</a></td>
<td><a href="#" class="w_1" onClick="command('÷')">÷</a></td>
<td><a href="#" class="w_1" onClick="command('x')">×</a></td>
</tr>
<tr>
<td><a href="#" class="w_1" onClick="command(7)">7</a></td>
<td><a href="#" class="w_1" onClick="command(8)">8</a></td>
<td><a href="#" class="w_1" onClick="command(9)">9</a></td>
<td><a href="#" class="w_1" onClick="command('-')">-</a></td>
</tr>
<tr>
<td><a href="#" class="w_1" onClick="command(4)">4</a></td>
<td><a href="#" class="w_1" onClick="command(5)">5</a></td>
<td><a href="#" class="w_1" onClick="command(6)">6</a></td>
<td><a href="#" class="w_1" onClick="command('+')">+</a></td>
</tr>
<tr>
<td><a href="#" class="w_1" onClick="command(1)">1</a></td>
<td><a href="#" class="w_1" onClick="command(2)">2</a></td>
<td><a href="#" class="w_1" onClick="command(3)">3</a></td>
<td rowspan="2">
<a href="#" class="w_3 etc" onClick="calc()">=</a>
</td>
</tr>
<tr>
<td colspan="2">
<a href="#" class="w_2 zero" onClick="command(0)">0</a>
</td>
<td><a href="#" class="w_1 spot" onClick="command('.')">.</a></td>
</tr>
</table>
</div>
</div>
<script type="text/javascript">
//猎取当前显现数据
function command(v){
var res=document.getElementById("result").value;//猎取当前显现的数据
if(v=='C'){
document.getElementById("result").value=0;//假如当前数值即是C则,显现为0
}else if(res==0)
{
document.getElementById("result").value=v;//假如当前数值即是0,则稳定
}else{
document.getElementById("result").value+=v;//不然给当前值追加
}
}
//应用索引获得运算符位置
function reindex(str)
{
if(str.indexOf('x')!=-1){
return str.indexOf('x');
}else if(str.indexOf('÷')!=-1){
return str.indexOf('÷');
}else if(str.indexOf('+')!=-1){
return str.indexOf('+');
}else if(str.indexOf('-')!=-1){
return str.indexOf('-');
}
}
//盘算结果
function calc(){
var str= document.getElementById("result").value;
var index=reindex(str);
var num1=parseFloat(str.substr(0,index));
var num2=parseFloat(str.substr(index+1));
if(str.indexOf('x')!=-1)
{
//乘法盘算
document.getElementById("result").value=num1*num2;
//alert();
}else if(str.indexOf('÷')!=-1){
//除法盘算
document.getElementById("result").value=num1/num2;
}else if(str.indexOf('+')!=-1){
//加法盘算
document.getElementById("result").value=num1+num2;
}else if(str.indexOf('-')!=-1){
//减法盘算
document.getElementById("result").value=num1-num2;
}
}
</script>
</body>
</html>