index.html文件内容
<!DOCTYPE html>
<input id="sum" type="button" value="测试" />
</body>
index.js文件内容
function sum() {
var button = document.getElementById("sum");
alert("abc");
button.onclick = function() {
var num1 = document.getElementById("num1");
var num2 = document.getElementById("num2");
var num3 = document.getElementById("num3");
alert("三个数字之和为:" + (parseInt(num1.value) + parseInt(num2.value) + parseInt(num3.value)));
}
}
function addLoadEvent(func) {
var oldLoad = window.oldLoad;
if (typeof oldLoad != 'function') {
window.onload = func;
} else {
window.oldLoad = function() {
oldLoad();
func();
}
}
}
addLoadEvent(sum);
```<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
var n1=document.getElementById("num1").value; //获取到的是字符串,不能用n1进行运算
var n2=document.getElementById("num2").value;
var n3=document.getElementById("num3").value;
console.log(typeof n1); //输出的是unfinished类型
function btn(){
alert(parseFloat(document.getElementById("num1").value)+parseFloat(document.getElementById("num2").value)+parseFloat(document.getElementById("num3").value));
}
</script>
</head>
<body>
<p>请输入三个数字:</p>
<form action="index.html" method="get">
<input type="text" id="num1" value="" />
<input type="text" id="num2" value="" />
<input type="text" id="num3" value="" />
<button type="button" onclick="btn();">测试</button>
</form>
</body>
</html>