易混淆点、易错点:this关键字

this

1、在普通函数中  this 就指 浏览器顶级对象  window

console.log(this)

2、在事件函数中   this指的是当前触发事件的元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<span class="txt">我不喜欢学习!</span>
<script>
    window.onload = function () {
        var t = document.querySelector('.txt');
        t.onclick = function () {
            this.innerText = '不,你喜欢!';
        }
    }
</script>
</body>
</html>

点击后:

猜你喜欢

转载自blog.csdn.net/weixin_42223833/article/details/88137780