javascript怎么设置元素点击事件

javascript怎么设置元素点击事件,第1张

javascript怎么设置元素点击事件

javascript设置元素点击事件的方法:1、创建一个HTML示例文件;2、添加script标签;3、通过getElementById获取button元素;4、通过“btn.onclick”方法设置元素点击事件即可。

本文 *** 作环境:windows7系统、javascript1.8.5版、DELL G3电脑

javascript怎么设置元素点击事件?

很简单,看下面的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        window.onload=function(){
            var btn = document.getElementById("btn");
            btn.onclick=function(){
            alert("这就是点击事件~");
        }
        }
       
    </script>
</head>
<body>
    <button id="btn">点击一下吧</button>
</body>
</html>

推荐学习:《javascript基础教程》

以上就是javascript怎么设置元素点击事件的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/693162.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-22
下一篇2022-04-22

发表评论

登录后才能评论

评论列表(0条)

    保存