怎么用HTML和javascript做出一个加减乘除计算器?
正在学javascript这是一本书上的练习请问这个该怎么做?能不能写一个HTML代码给我呢?...
正在学javascript
这是一本书上的练习
请问这个该怎么做?
能不能写一个HTML代码给我呢? 展开
这是一本书上的练习
请问这个该怎么做?
能不能写一个HTML代码给我呢? 展开
2个回答
2015-06-09 · 百度知道合伙人官方认证企业
关注
展开全部
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
function jiaFun() {
var oneValue = document.getElementById('oneTxt').value;
var twoValue = document.getElementById('twoTxt').value;
document.getElementById('valueTxt').value = parseFloat(oneValue) + parseFloat(twoValue);
}
function jianFun() {
var oneValue = document.getElementById('oneTxt').value;
var twoValue = document.getElementById('twoTxt').value;
document.getElementById('valueTxt').value = parseFloat(oneValue) - parseFloat(twoValue);
}
function chengFun() {
var oneValue = document.getElementById('oneTxt').value;
var twoValue = document.getElementById('twoTxt').value;
document.getElementById('valueTxt').value = parseFloat(oneValue) * parseFloat(twoValue);
}
function chuFun() {
var oneValue = document.getElementById('oneTxt').value;
var twoValue = document.getElementById('twoTxt').value;
document.getElementById('valueTxt').value = parseFloat(oneValue) / parseFloat(twoValue);
}
</script>
</head>
<body>
第一个数:<input type="text" id="oneTxt" /><br />
第二个数:<input type="text" id="twoTxt" /><br /><br />
<input type="button" value="+" style="width:60px" onclick="jiaFun()" />
<input type="button" value="-" style="width:60px" onclick="jianFun()" />
<input type="button" value="×" style="width:60px" onclick="chengFun()" />
<input type="button" value="÷" style="width:60px" onclick="chuFun()" /><br /><br />
计算结果:<input type="text" id="valueTxt" />
</body>
</html>
自己写的哦 有问题欢迎咨询
展开全部
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<table>
<tr>
<td><input type="button" value="add" onclick="setOp('+', 'add');"/></td>
<td><input type="button" value="miner" onclick="setOp('-', 'miner');"/></td>
<td><input type="button" value="times" onclick="setOp('*', 'times');"/></td>
<td><input type="button" value="divide" onclick="setOp('/', 'divide');"/></td>
</tr>
</table>
<table id="tb_calc" style="display:none;">
<tr>
<td> <input id="x" type="text" style="width:100px" value="" name="x" /></td>
<td> <lable id="op" name="op"></lable> </td>
<td> <input id="y" type="text" style="width:100px" value="" name="y" /> </td>
<td> <input id="opTips" type="button" value="" onclick="calc();"/> </td>
<td> <lable id="z" name="z"></lable> </td>
</tr>
</table>
<script type="application/javascript">
function setOp(op, opTips)
{
var tb=document.getElementById("tb_calc");
tb.style.display = "none";
document.getElementById("x").value = "";
document.getElementById("y").value = "";
document.getElementById("z").innerText = "";
document.getElementById("op").innerText = op;
document.getElementById("opTips").value = opTips;
tb.style.display = "block";
}
function calc()
{
var x = parseInt(document.getElementById("x").value);
var y = parseInt(document.getElementById("y").value);
var op = document.getElementById("op").innerText;
var z = "";
switch(op)
{
case '+':
z = x + y;
break;
case '-':
z = x - y;
break;
case '*': ;
z = x * y;
break;
case '/': ;
z = x / y;
break;
default:
z = '';
}
console.log(x, op, y, '=', z);
document.getElementById("z").innerText = z;
}
</script>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<table>
<tr>
<td><input type="button" value="add" onclick="setOp('+', 'add');"/></td>
<td><input type="button" value="miner" onclick="setOp('-', 'miner');"/></td>
<td><input type="button" value="times" onclick="setOp('*', 'times');"/></td>
<td><input type="button" value="divide" onclick="setOp('/', 'divide');"/></td>
</tr>
</table>
<table id="tb_calc" style="display:none;">
<tr>
<td> <input id="x" type="text" style="width:100px" value="" name="x" /></td>
<td> <lable id="op" name="op"></lable> </td>
<td> <input id="y" type="text" style="width:100px" value="" name="y" /> </td>
<td> <input id="opTips" type="button" value="" onclick="calc();"/> </td>
<td> <lable id="z" name="z"></lable> </td>
</tr>
</table>
<script type="application/javascript">
function setOp(op, opTips)
{
var tb=document.getElementById("tb_calc");
tb.style.display = "none";
document.getElementById("x").value = "";
document.getElementById("y").value = "";
document.getElementById("z").innerText = "";
document.getElementById("op").innerText = op;
document.getElementById("opTips").value = opTips;
tb.style.display = "block";
}
function calc()
{
var x = parseInt(document.getElementById("x").value);
var y = parseInt(document.getElementById("y").value);
var op = document.getElementById("op").innerText;
var z = "";
switch(op)
{
case '+':
z = x + y;
break;
case '-':
z = x - y;
break;
case '*': ;
z = x * y;
break;
case '/': ;
z = x / y;
break;
default:
z = '';
}
console.log(x, op, y, '=', z);
document.getElementById("z").innerText = z;
}
</script>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询