JavaScript事件对象

如何获取事件对象?

在给元素绑定事件的时候,在事件的function函数的参数列表中添加一个参数---习惯取名为event,这个event就是事件对象

<!DOCTYPE html>
<html>
    <head>
    <meta charset="UTF-8">
    <style>
        div#a{
            margin-bottom: 10px;
            border:1px solid black;
            100px;
            height:100px;
        }
        div#b{
            border:1px solid black;
            200px;
            height:100px;
        }
    </style>
    <!--引入jQuery库-->
    <script type="text/javascript" src="../lib/jquery-1.7.2.js"></script>
    <script type="text/javascript">
      
    $(function(){
        //原生js获取事件对象
        // window.onload=function(){
        //     document.getElementById("a").onclick=function(event){
        //         console.log(event);
        //     }
        // }
        //jQuery代码获取事件对象
        // $(function(){
        //     $("#b").click(function(event){
        //         console.log(event);
        //     });
        // });

        $(function(){
            $("#a").bind("mouseover mouseout",function(event){
                if(event.type=="mouseover"){
                    console.log("鼠标移入");
                }
                else{
                    console.log("鼠标移出");
                }
            })
        });
    });
    </script>
    </head>
    <body>
        <div id="a"></div>
        <div id="b"></div>
    </body>
</html>

原文地址:https://www.cnblogs.com/fate-/p/14706535.html