第一种方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function test(){
console.log("HelloWorld")
}
</script>
</head>
<body>
<input type="button" value=按钮 id="button" onclick="test()"/>
</body>
</html>
第二种方式:
<!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>
第三种方式:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value=按钮 id="button"/>
<script>
document.getElementById("button").addEventListener("click",function(){
console.log("HelloWorld")
})
</script>
</body>
</html>
本文介绍了三种在HTML中使用JavaScript实现按钮点击事件的方法:直接在HTML中设置onclick属性调用函数;在JavaScript中通过getElementById获取元素并设置onclick属性;使用addEventListener监听click事件。这些方法适用于前端开发中的交互设计。
426

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



