首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  HTML/JS/CSS技术专区 » 自己写的一个JavaScript网页计算器源码(附效果图)  


  作者:一起PHP
  注册时间:2004-02-27
  主题/回复:247/1521
  积分:4649
  等级:★★★★★☆(十一级)
  称号:论坛圣人

  nqp@nqp.me
  4304410
  www.nqp.me

 

 发表:2008-03-27 09:43:53 阅读 4310 次 回复 5 次 得分6  |   字号 字色
自己写的一个JavaScript网页计算器源码(附效果图)
上课时给学生做的一个Javascript计算器例子,有兴趣的朋友可以研究一下。

代码如下:
<head>
<style type=text/css>
TD.title{
  font-size:12px;
  color:#FFFFFF;
  background-color:#000000;
  text-align:center;
}
INPUT.number{
  width:50px;
  color:#FF0000;
}
INPUT.operater{
  width:50px;
  color:#0000FF;
}
</style>
<script language=javascript>
function getnum(num){
  if(parseFloat(disp.value)!=0)
  disp.value = disp.value + num;
  else
  disp.value = num;
}
function jisuan(){
  result = eval(disp.value);
  disp.value = result;
}

</script>
</head>
<body bgcolor=#000000>
<table border=1 bordercolor="pink" width=200 align=center 

bgcolor=pink>
<tr>
<td colspan=4 class=title>自己做个计算器</font></td>
</tr>
<tr>
<td colspan=4><input type=text name=disp value="0" style="text-

align:right;width:215px;color:#0000FF"></td>
</tr>
<tr>
<td><input type=button value=0 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value=1 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value=2 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value=+ class="operater" onclick="getnum

(this.value)"></td>
</tr>
<tr>
<td><input type=button value=3 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value=4 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value=5 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value=- class="operater" onclick="getnum

(this.value)"></td>
</tr>
<tr>
<td><input type=button value=6 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value=7 class="number"" onclick="getnum

(this.value)"></td>
<td><input type=button value=8 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value=* class="operater" onclick="getnum

(this.value)"></td>
</tr>
<tr>
<td><input type=button value=9 class="number" onclick="getnum

(this.value)"></td>
<td><input type=button value="(" class="operater" onclick="getnum

(this.value)"></td>
<td><input type=button value=")" class="operater" onclick="getnum

(this.value)"></td>
<td><input type=button value="." class="operater" onclick="getnum

(this.value)"></td>
</tr>
<tr>
<td><input type=button value="Help" style="width:50px" 

onclick="alert('程序制作:17php.com')"></td>
<td><input type=button value="C" class="operater" 

onclick="disp.value='0'"></td>
<td><input type=button value="=" class="operater" onclick="jisuan

()"></td>
<td><input type=button value="/" class="operater" onclick="getnum

(this.value)"></td>
</tr>
<tr><td colspan=4 class=title>版权没有 欢迎试用</td></tr>
</table>


直接复制代码,存为html即可查看效果。
运行效果见附图。

相关附件如下:

 
 1#楼  
 
  回复人:lc0633
  注册时间:2007-08-08
  主题/回复:1/8
  积分:427
  等级:★★☆(五级)
  称号:略有小成

   
 1#楼 发表于2008-06-01 10:01:09  评分:2 

回复给楼主(一起PHP)
 2#楼  
 
  回复人:温柔的屠夫
  注册时间:2008-06-10
  主题/回复:0/1
  积分:22
  等级:★(二级)
  称号:初出茅庐

   
 2#楼 发表于2008-06-10 14:00:37  评分:1 

回复给楼主(一起PHP)
 3#楼  
 
  回复人:coolboy0316
  注册时间:2008-03-11
  主题/回复:19/44
  积分:367
  等级:★★(四级)
  称号:崭露头角

   
 3#楼 发表于2008-09-20 13:36:08  评分:1 

回复给楼主(一起PHP)
不错啊,聂老师!
加油
 4#楼  
 
  回复人:wsswan
  注册时间:2009-11-10
  主题/回复:1/3
  积分:39
  等级:★(二级)
  称号:初出茅庐

   
 4#楼 发表于2009-11-10 10:25:04  评分:1 

回复给楼主(一起PHP)牛人~
 5#楼  
 
  回复人:邓文兵
  注册时间:2012-01-22
  主题/回复:0/1
  积分:24
  等级:★(二级)
  称号:初出茅庐

   
 5#楼 发表于2012-01-22 16:53:04  评分:1 

那些按键家上<form>标记,为什么不可以了,disp变量为什么可以直接用,
为什么用 var disp=getElementByName("disp");也不行啊,求解?
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

一起PHP技术联盟 主办:一起PHP 联系方式:站长QQ:4304410 QQ群:8423742 20159565 站长博客 E-mail: nqp@nqp.me 执行时间:0.01sec
SimsmaBBS 2008 (v6.0) Developed by 17php.com,Copyright(C)2003-2010 All rights reserved. 副本授权:一起PHP官方专用版