欢迎来到HELLO素材网!
丰富的DIV CSS模版、JS,jQuery特效免费提供下载
当前位置:主页 > 建站教程 > JS教程 >

JavaScript的Math对象计算器

发表于2014-06-03 21:32| 次阅读| 来源整理| 作者管理员

摘要:本例用一个计算器的形式练习了JavaScript的Math对象(数学对象)的使用。

JavaScript代码

<script type="text/javascript">
var calc = {
 first:0,
 result:0,
 init:function(){
  this.first = parseFloat(document.getElementById("first").value);
 },
 setResult:function(){document.getElementById("result").value = this.result},
 abs:function(){ 
   this.init();
   this.result = Math.abs(this.first);
   this.setResult();
   },
 sin:function(){
   this.init();
   this.result = Math.sin(this.first/360 * Math.PI);
   this.setResult();
 },
 cos:function(){
   this.init();
   this.result = Math.cos(this.first/360 * Math.PI);
   this.setResult();
 },
 tan:function(){
   this.init();
   this.result = Math.tan();
   this.setResult();
 },
 round:function(){
   this.init();
   this.result = Math.round(this.first);
   this.setResult();
 },
 ceil:function(){
   this.init();
   this.result = Math.ceil(this.first);
   this.setResult();
 },
 exp:function(){
   this.init();
   this.result = Math.exp(this.first);
   this.setResult();
 },
 floor:function(){
   this.init();
   this.result = Math.floor(this.first);
   this.setResult();
 },
 log:function(){
   this.init();
   this.result = Math.log(this.first);
   this.setResult();
 },
 E:function(){
   this.result = Math.E;
   this.setResult();
 },
 PI:function(){
   this.result = Math.PI;
   this.setResult();
 },
 SQRT2:function(){
   this.result = Math.SQRT2;
   this.setResult();
 },
 SQRT1_2:function(){
   this.result = Math.SQRT1_2;
   this.setResult();
 },
 LN2:function(){
   this.result = Math.LN2;
   this.setResult();
 },
 LN10:function(){
   this.result = Math.LN10;
   this.setResult();
 },
 LOG2E:function(){
   this.result = Math.LOG2E;
   this.setResult();
 },
 LOG10E:function(){
   this.result = Math.LOG10E;
   this.setResult();
 }
};
</script>  
HTML代码
<form id="jisuanqi" action="#">
<p>
<label for="first">数字:</label>
<input id="first" type="text" />
</p>
<p>
<label for="result">结果:</label>
<input id="result" type="text" disabled="disabled" />
</p>
<button onclick="calc.abs();;return false;" value="绝对值" >绝对值</button>
<button onclick="calc.sin();;return false;" value="sin" >sin</button>
<button onclick="calc.cos();;return false;" value="cos" >cos</button>
<button onclick="calc.tan();;return false;" value="tan" >tan</button>
<button onclick="calc.round();;return false;" value="round" >round</button>
<button onclick="calc.ceil();;return false;" value="ceil" >ceil</button>
<button onclick="calc.exp();;return false;" value="exp" >exp</button>
<button onclick="calc.log();;return false;" value="log" >log</button>
<button onclick="calc.floor();;return false;" value="floor" >floor</button>
<button onclick="calc.floor();;return false;" value="floor" >floor</button>
<br />
<button onclick="calc.E();;return false;" value="E" >E</button>
<button onclick="calc.PI();;return false;" value="PI" >PI</button>
<button onclick="calc.SQRT2();;return false;" value="SQRT2" >SQRT2</button>
<button onclick="calc.SQRT1_2();;return false;" value="SQRT1_2" >SQRT1_2</button>
<button onclick="calc.LN2();;return false;" value="LN2" >LN2</button>
<button onclick="calc.LN10();;return false;" value="LN10" >LN10</button>
<button onclick="calc.LOG2E();;return false;" value="LOG2E" >LOG2E</button>
<button onclick="calc.LOG10E();;return false;" value="LOG10E" >LOG10E</button>
</form> 

效果(可以自己美化一下使用哦)



运行代码保存代码提示:您可以先修改部分代码再运行