码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript中点击(click)事件的3种写法

时间:2019-02-11 20:15:12      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:func   dde   false   doctype   pre   nbsp   写法   world   ext   

 

方法一

<!DOCTYPE html>
<html>
<head>
    <title>Javascript中点击事件方法一</title>
</head>
<body>
    <button id="btn">click</button>
    <script type="text/javascript">
        var btn = document.getElementById("btn");
        btn.onclick=function(){
            alert("hello world");
        }
    </script>
</body>
</html>

 

方法二

<!DOCTYPE html>
<html>
<head>
    <title>Javascript中点击事件方法二</title>
</head>
<body>
    <button id="btn">click</button>
    <script type="text/javascript">
        var btn = document.getElementById("btn");
        btn.addEventListener(click,function(){
            alert("hello wrold");
        },false)
    </script>
</body>
</html>

 

方法三

<!DOCTYPE html>
<html>
<head>
    <title>Javascript中点击事件方法三</title>
    <script type="text/javascript">
        function test(){
            alert("hello world");
        }
    </script>
</head>
<body>
    <button id="btn" onclick="test()">click</button>
</body>
</html>

 

Javascript中点击(click)事件的3种写法

标签:func   dde   false   doctype   pre   nbsp   写法   world   ext   

原文地址:https://www.cnblogs.com/sea-stream/p/10362815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!