zoukankan      html  css  js  c++  java
  • 句柄式监听document.getElementById("btn").addEventListener("click",function(){})

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <p id="div">hello</p>
        <button onclick="demo()">按钮1</button>
        <button id="btn">按钮2</button>
        <button id="btn_many">添加多个句柄事件</button>
        <script>
            // 以前的监听
            function demo(){
                document.getElementById("div").innerHTML = "改变"
            }
            // 句柄式监听
            document.getElementById("btn").addEventListener("click",
                function(){
                    alert("句柄监听事件");
                }
            );  
            // 声明多个句柄
            var x = document.getElementById("btn_many");
            x.addEventListener("click",hello);
            x.addEventListener("click",world);
            // 移出句柄
            x.removeEventListener("click",hello);
            function hello(){
                alert("hello")
            }
            function world(){
                alert("world")
            }
        </script>
    </body>
    </html>
  • 相关阅读:
    [SQL]数据更新
    Hadoop之mapred
    hadoop之hdfs
    HTML —— 11.25日总结
    HTML —— 11.19日的总结
    三个爬虫的小栗子
    HTML —— video标签
    py爬虫 —— 三个爬虫的小栗子
    HTML —— img标签
    py爬虫 —— py爬虫requests
  • 原文地址:https://www.cnblogs.com/fdxjava/p/13184866.html
Copyright © 2011-2022 走看看