计算器功能实现
2015-11-13 19:26
495 查看
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>计算器界面</title>
<style type="text/css">
*{margin:0px; padding:0px;}
table{
display:block;
margin:50px auto;
width:200px;
height:160px;
box-shadow:2px 2px black;
border-radius:8px;
}
input{
width:200px;
height:30px;
text-align:right;
border-radius:8px;
font-size:20px;
font-family: Times, TimesNR, 'New Century Schoolbook',Georgia, 'New York', serif;
}
button{
width:49px;
height:40px;
font-size:18px;
background-color:white;
border-radius:8px;
font-style:oblique;
font-family: Times, TimesNR, 'New Century Schoolbook',Georgia, 'New York', serif;
}
button:hover{
background-color:#ff9999;
}
</style>
</head>
<body>
<table id="table" border="1" cellspacing="0">
<tr>
<td colspan="4"><input id="input"/></td>
</tr>
</table>
<script type="text/javascript">
window.onload = function(){
var keys = ["7","8","9","*","4","5","6","/","1","2","3","-","0","+","clear","="];
var t=0;
var table = document.getElementById("table");
for(var i=0; i<4; i++){
var tr = table.insertRow(-1);
for(var j=0; j<4; j++,t++){
tr.insertCell(-1).innerHTML = "<button value="+keys[t]+" onclick='inputEvent(this)'>"+keys[t]+"</button>";
}
}
}
function inputEvent(a){
inp = document.getElementById("input");
if(a.value=="="){
inp.value = eval(inp.value);
return;
}
if(a.value=="clear"){
inp.value = "";
return;
}
inp.value += a.value;
}
</script>
</body>
</html>
<head>
<meta charset="UTF-8">
<title>计算器界面</title>
<style type="text/css">
*{margin:0px; padding:0px;}
table{
display:block;
margin:50px auto;
width:200px;
height:160px;
box-shadow:2px 2px black;
border-radius:8px;
}
input{
width:200px;
height:30px;
text-align:right;
border-radius:8px;
font-size:20px;
font-family: Times, TimesNR, 'New Century Schoolbook',Georgia, 'New York', serif;
}
button{
width:49px;
height:40px;
font-size:18px;
background-color:white;
border-radius:8px;
font-style:oblique;
font-family: Times, TimesNR, 'New Century Schoolbook',Georgia, 'New York', serif;
}
button:hover{
background-color:#ff9999;
}
</style>
</head>
<body>
<table id="table" border="1" cellspacing="0">
<tr>
<td colspan="4"><input id="input"/></td>
</tr>
</table>
<script type="text/javascript">
window.onload = function(){
var keys = ["7","8","9","*","4","5","6","/","1","2","3","-","0","+","clear","="];
var t=0;
var table = document.getElementById("table");
for(var i=0; i<4; i++){
var tr = table.insertRow(-1);
for(var j=0; j<4; j++,t++){
tr.insertCell(-1).innerHTML = "<button value="+keys[t]+" onclick='inputEvent(this)'>"+keys[t]+"</button>";
}
}
}
function inputEvent(a){
inp = document.getElementById("input");
if(a.value=="="){
inp.value = eval(inp.value);
return;
}
if(a.value=="clear"){
inp.value = "";
return;
}
inp.value += a.value;
}
</script>
</body>
</html>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- JavaScript 各种遍历方式详解
- call/apply/bind 的理解与实例分享
- IE8开发人员工具教程(二)
- 在flex中执行一个javascript方法的简单方式
- Flex结合JavaScript读取本地路径的方法
- Ruby on Rails中jquery_ujs组件拖慢速度的问题解决