HTML添加事件的方式
1.使用属性onclick,在表头定义函数
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function test(){
console.log("HelloWorld");
}
</script>
</head>
<body>
<input type="button" value="按钮" onclick="test()"/>
</body>
</html>
2.使用document.getElementById来添加事件的监听
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="按钮" id="button"/>
<script>
document.getElementById("button").onclick=function(){
console.log("HelloWorld");
}
</script>
</ body>
</html>
3.使用addEventListener添加事件监听
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="按钮" id="button"/>
<script>
document.getElementById("button").addEventListener("button",test=function(){
console.log("HelloWorld");
});
</script>
</body>
</html>
以上的三种方式都可以用来给标签添加事件监听,但是相对于其他两种方式来说,第三种使用的频率更高。

3931

被折叠的 条评论
为什么被折叠?



