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

方法一

<!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>

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/10362815.html