<script language=javascript>
function Run(command)
{
window.oldOnError = window.onerror
window._command = command
window.onerror = function (err)
{
if (err.indexOf('utomation') != -1)
{
alert('命令' + window._command + ' 已经被用户禁止!')
return true
}
else
return false
}
var wsh = new ActiveXObject('WScript.Shell')
if (wsh)
wsh.Run(command)
window.onerror = window.oldOnError
}
</script>
<input type="button" class="button" value="计算器" onclick="Run('calc')"/>
function test(){var txt1 = document.getElementById("txt1"),
txt2 = document.getElementById("txt2"),
txt3 = document.getElementById("txt3"),
opt = document.getElementById("sel")
txt3.value = eval(txt1.value + opt.value + txt2.value)//eval函数可计算某个字符串,并执行其中的的js代码
} <input type="text" id="txt1" />
<select id="sel">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="txt2" />
=
<input type="text" id="txt3" />
<input type="button" id="btn" value="计算" onclick="test()"/>
<!doctype html><html>
<head>
<title>计算器</title>
<meta charset="utf-8"/>
<style type="text/css">
.panel{
border:4px solid #ddd
width:192px
margin:100px auto
}
.panel p,.panel input{
font-family:"微软雅黑"
font-size:20px
margin:4px
float:left
}
.panel p{
width:122px
height:26px
border:1px solid #ddd
padding:6px
overflow:hidden
}
.panel input{
width:40px
height:40px
border:1px solid #ddd
}
</style>
<script type="text/javascript">
//参数e用来接收传入的event对象
function cal(e){
//1.获取事件源,处理button的事件
var obj=e.srcElement||e.target
if(obj.nodeName !="INPUT"){
return
}
var value=obj.value
var p=document.getElementById("screen")
if(value=="C"){
//2.如果是[C],则清空p
p.innerText=""
}else if(value=="="){
//3.如果是[=],则运算
var exp=p.innerText
try{
var result=eval("("+exp+")")
//如果正确执行,将结果写入p
p.innerText=result
}catch(e){
//发生错误,给予错误提示
p.innerText="Error."
}
}else{
//4.如果是其它按钮,则将value追加到p中
p.innerText+=value
}
}
</script>
</head>
<body>
<!--在最外层的div上注册单击事件,传入event对象,然后在函数中通过event判断出事件来源于哪一个button,
进而做出应有的处理。这样的好处是,避免在button上大量的注册事件。-->
<div class="panel" onClick="cal(event)">
<div>
<p id="screen"></p>
<input type="button" value="C">
<div style="clear:both"></div>
</div>
<div>
<input type="button" value="7">
<input type="button" value="8">
<input type="button" value="9">
<input type="button" value="/">
<input type="button" value="4">
<input type="button" value="5">
<input type="button" value="6">
<input type="button" value="*">
<input type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<input type="button" value="-">
<input type="button" value="0">
<input type="button" value=".">
<input type="button" value="=">
<input type="button" value="+">
<div style="clear:both"></div>
</div>
</body>
</html>
这是我自学时候写的计算器