JS编写 不用第三个值交换两个数
需要用到一个脑筋急转弯:a=a+b; b=a-b; a=a-b;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<script>
function change(){
var a=document.getElementById("num1").value*1;
var b=document.getElementById("num2").value*1;
var a=a+b;
var b=a-b;
var a=a-b;
document.getElementById("rst").value="a的值为:"+a+" ; b的值为:"+b;
}
</script>
<style>
.font1{
font-family: "微软雅黑";
font-size:40px;
}
#num1{
border: 0.5px solid #5891FE;
border-radius: 5px;
width: 500px;
height: 40px;
font-size: 30px;
}
#num2{
border: 0.5px solid #5891FE;
border-radius: 5px;
width: 500px;
height: 40px;
font-size: 30px;
font-family: "微软雅黑";
}
#rst{
border: 0.5px solid #5891FE;
border-radius: 5px;
width: 500px;
height: 40px;
font-size: 30px;
font-family: "微软雅黑";
}
#button1{
border: 1px solid grey;
width:100px;
height: 50px;
font-size:20px;
font-family: "微软雅黑";
font-weight: bold;
}
</style>
<body><br><br><br><br><br><br><br>
<table width="100%" border="0">
<tbody>
<tr><td align="center"><font class="font1">不采用第三个值的情况下交换两个数的值</font></td></tr>
<tr>
<td align="center"><b class="font1">请输入第一个数:</b><input type="text" name="first num" id="num1"></td>
</tr>
<tr>
<td align="center"><b class="font1">请输入第二个数:</b><input type="text" name="second num" id="num2"></td>
</tr>
<tr>
<td align="center"><b class="font1">交换两个数的值:</b><input type="text" name="result" id="rst" readonly></td>
</tr>
<tr>
<td align="center"><br><input type="button" value="start!" onClick="change()" id="button1"></td>
</tr>
</tbody>
</table>
</body>
</html>