1、通过onclick()单击事件调用
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<input type="button" value="test1" id="btn1" onclick="fun1()">
</body>
</html>
JS代码:
function fun1(){
alert("调用一");
}
网页效果:
function fun1(){
alert("hello");
}
function fun2(){
// 通过对象
document.getElementById("btn2").onclick=function(){
alert("world");
}
}
2、通过onload()加载事件调用
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<script type="text/javascript" src="index.js"></script>
</head>
<!-- onload()页面加载、刷新、初始化执行里面的函数 -->
<body onload="fun2()">
<input type="button" value="test2" id="btn1">
</body>
</html>
js代码:
function fun2(){
alert("调用二");
}
网页效果:
小结
- 第一个按钮通过οnclick="fun1()"进行调用
- 第二个按钮通过对象ID,在body里面使用οnlοad="fun2()"进行调用