事件函数中this指向

https://img2.mukewang.com/5c21e0480001c00307270328.jpg

<!DOCTYPE HTML>

<html>

<head>

    <meta charset="UTF-8">

    <title></title>

</head>

<body>

    

    <h2 onmousedown = "f1(this)">事件中的this</h2>


    <script type="text/javascript">

        

        var h2 = document.getElementsByTagName('h2')[0];

        

        //HTML方式绑定

        function f1(obj){

            console.log(obj);

        }

        f1( this );

        

        /*

        //DOM 0级绑定方式

        h2.onclick = function(){

            console.log(this);

        }

        

        //DOM 2级方式

        h2.addEventListener('mouseover',function(){

            console.log(this);

        });

        */

    </script>

    


</body>

</html>


慕哥6287543
浏览 477回答 1
1回答

jeck猫

javascript的this跟函数定义在哪里无关,跟谁调用它有关。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript