<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js加减乘除计算器代码</title>
<style>
body,ul{ margin:0pxpadding:0px}
body{ background:#AF6332}
li{ list-style:none}
.fl{ float:left}
.fr{ float:right}
.clearfix:after{ content:""display:blockclear:both}
.clearfix{zoom:1}
/*是用inset可以将外部阴影改成内部阴影若要实现内外阴影同时存在,将其并在一行用逗号隔开*/
.calBox{ width:460pxpadding-bottom:10pxbackground:#FDFDFDborder-radius:5pxposition:absoluteleft:50%top:50%margin-left:-230pxmargin-top:-225pxbox-shadow:0px 0px 10px rgba(0,0,0,0.8),0px 0px 10px rgba(0,0,0,0.5) inset-webkit-box-shadow:0px 0px 10px rgba(0,0,0,0.8),0px 0px 10px rgba(0,0,0,0.5) insetbackground:#F9F9F9overflow:hidden}
input{ width:406pxheight:82pxmargin:10px 7px 0pxborder-radius:5pxborder:1px solid #64655Fbox-shadow:0px 5px 2px rgba(157,157,145,0.8) inset-webkit-box-shadow:0px 5px 2px rgba(157,157,145,0.8) insetoutline:nonebackground:#FCFDEBtext-align:rightfont-family:"微软雅黑"font-size:40pxpadding:0px 10px}
ul{}
li{ list-style:nonewidth:74pxheight:34pxline-height:34pxtext-align:centerfont-family:"微软雅黑"border:1px solid #8B8B8Bborder-radius:5pxbackground:url(/jscss/demoimg/201402/calBg) repeat-xfloat:leftmargin:12px 6px 0px}
.one li{ height:44pxbackground:url(/jscss/demoimg/201402/calBg1.jpg) repeat-xline-height:44pxcursor:pointer}
.one .orange{ background:url(/jscss/demoimg/201402/calBg2.jpg) repeat-xborder:1px solid #875733}
.one .black{ background:url(/jscss/demoimg/201402/calBg3.jpg) repeat-xborder:1px solid #363636color:#fff}
.one .gray{ background:url(/jscss/demoimg/201402/calBg4.jpg) repeat-xborder:1px solid #5F6366}
.zero{ width:160px}
.one .deng{ background:url(/jscss/demoimg/201402/calBg5.jpg)height:100px}
.twoBox{ width:353pxoverflow:hidden}
.two{ width:358px}
.calBox .three { margin:0px}
.calu{ padding:0px 10pxwidth:470px}
</style>
<script type="text/javascript">
function findArr(a,c){for(var b=0b<a.lengthb++){if(a[b]==c){return true}}return false}function getClass(d,f){if(document.getElementsByClassName){return d.getElementsByClassName(f)}else{var a=[]var e=document.getElementsByTagName("*")for(var c=0c<e.lengthc++){var b=e[c].className.split(" ")if(findArr(b,f)){a.push(e[c])}}return a}}
window.onload=function()
{
var aNum=getClass(document,'num')
var oText=document.getElementById('text')
var aPer=getClass(document,'oper')
var oPer=document.getElementById('per')
var oText1=document.getElementById('text1')
var oDeng=getClass(document,'deng')[0]
var oSq=getClass(document,'sq')[0]
var oRec=getClass(document,'rec')[0]
var oZheng=getClass(document,'zheng')[0]
var oOn=getClass(document,'on')[0]
var oOff=getClass(document,'off')[0]
var oClea=getClass(document,'clea')[0]
var bOnOrOffClick=false
function fnNum(a)
{
var bClear=false
oText.value='0'
for(var i=0i<aNum.lengthi++)
{
aNum[i].onclick=function()
{
if(!bOnOrOffClick)return
if(bClear)
{
bClear=false
}
if(oText.value.indexOf('.')!=-1)
{
if(this.innerHTML=='.')
{
return
}
}
if(oPer.value&&oText.value&&oText1.value=='')
{
oText1.value=oText.value
oText.value=''
}
var re=/^0\.{1}\d+$/
var re1=/^([0]\d+)$/
oText.value+=this.innerHTML
if(re.test(oText.value))
{
return
}
if(re1.test(oText.value))
{
oText.value=this.innerHTML
}
}
//符号部分的添加
for(var j=0j<aPer.lengthj++)
{
aPer[j].onclick=function()
{
if(oText.value&&oPer.value&&oText1.value)
{
var n=eval(oText1.value+oPer.value+oText.value)
oText.value=n
oText1.value=''
}
oPer.value=this.innerHTML
}
}
//点击等号的时候
oDeng.onclick=function()
{
//+-*/%的情况
if(oText1.value==''&&oPer.value==''&&oText.value=='')
{
return
}
var n=eval(oText1.value+oPer.value+oText.value)
oText.value=n
oText1.value=''
oPer.value=''
bClear=true
}
//点击开根号的时候
oSq.onclick=function()
{
var m=Math.sqrt(oText.value)
oText.value=m
}
//点击倒数的时候
oRec.onclick=function()
{
var a=1/oText.value
if(oText.value=='0')
{
a='正无穷'
}
oText.value=a
}
//正负号的时候
oZheng.onclick=function()
{
if(oText.value>0)
{
oText.value=-oText.value
}
else
{
oText.value =-oText.value
}
}
//清屏的时候
oClea.onclick=function()
{
oText.value='0'
oText1.value=''
oPer.value=''
}
}
}
//on时
oOn.onclick=function()
{
bOnOrOffClick=true
fnNum(bOnOrOffClick)
}
//off时
oOff.onclick=function()
{
bOnOrOffClick=false
fnNum(bOnOrOffClick)
oText.value=''
}
}
</script>
</head>
<body>
<div class="calBox">
<div class="calu">
<input type="text" id="text">
<ul class="one clearfix">
<li class="orange on">开机</li>
<li class="orange off">关机</li>
<li class="orange clea">清屏</li>
<li class="black zheng">+/-</li>
<li class="black rec">1/x</li>
<li class="num">7</li>
<li class="num">8</li>
<li class="num">9</li>
<li class="gray oper">/</li>
<li class="black oper">%</li>
<li class="num">4</li>
<li class="num">5</li>
<li class="num">6</li>
<li class="gray oper">*</li>
<li class="black sq">√</li>
<!-- -->
</ul>
<div class="clearfix">
<div class="twoBox fl">
<ul class="one fl two">
<li class="num">1</li>
<li class="num">2</li>
<li class="num">3</li>
<li class="gray oper">-</li>
<li class="zero num">0</li>
<li class="num">.</li>
<li class="gray oper">+</li>
</ul>
</div>
<ul class="one three clearfix fl">
<li class="black deng fl">=</li>
</ul>
</div>
</div>
</div>
<input type="text" id="per" style="display:none">
<input type="text" id="text1" style="display:none">
<div style="text-align:centerclear:both">
</div>
</body>
</html>
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>计数器</title>
</head>
<body>
<input type="text" name="text" id="pre" onblur="validate(this.value)">
<select id="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="text" id="next" onblur="validate(this.value)">
<span>=</span>
<input type="text" id="result" readonly="true">
<input type="button" id="btn" value="提交" onclick="calculator()">
<script>
function validate(str){
var reg = /^\d+$/
if (!reg.test(str)) {
alert("请输入数字")
}
}
function calculator(){
var pre=document.getElementById("pre").value
var next=document.getElementById("next").value
var opra=document.getElementById("operator").value
var result=0
switch(opra) {
case "+":
result=parseInt(pre)+parseInt(next)
break
case "-":
result=parseInt(pre)-parseInt(next)
break
case "*":
result=parseInt(pre)*parseInt(next)
break
case "/":
if(parseInt(next)!=0){
result=parseInt(pre)/parseInt(next)
}
else{
alert("除数不能为0")
return
}
break
default:
break
}
document.getElementById("result").value=result
}
</script>
</body>
</html>
.首先我们要做好一个计算器的界面,主要用到html与css的知识,下面是代码0
1
2
3
+
c
4
5
6
-
=
7
8
7
*
+/-
0
.
/
上面设置一个简单的table表格,主要内容是计算器的数字键盘和符号,还有就是class名和ID名,函数名,主要是用作css样式设计和javascript程序设计,后面会用到,就先贴出来了
colspan="5" 是合并五列的意思,表示这个单元格要占五列
rowspan="3"是合并三行的意思,表示这个单元格要占三行
效果如下,这样一个简单的架构就完成了