Body、frameset的事件处理器被添加到全局window对象上的独特规则

起源是stackoverflow上的一个提问:在body上设置事件处理器(event handler),结果其却被添加到了全局window对象上。

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        body {
            height: 1000px;
        }
    </style>
    <title>Scroll</title>
</head>
<body>
    <button id="btn">click</button>
    <script type="text/javascript">
        document.body.onscroll = function() {
            alert(this);// displays [object Window], instead of [object HTMLBodyElement]
        };

        document.getElementById('btn').onclick = function() {
            alert(this);// displays [object HTMLButtonElement]
        }
    </script>
</body>
</html>

答案在WHATWG的标注规范上可以找到:

Most of the time, the object that exposes an event handler is the same as the object on which the corresponding event listener is added. However, the body and frameset elements expose several event handlers that act upon the element's Window object, if one exists. 

来源:片段链接

大部分时候,允许添加事件处理器的对象,与之后被添加相应事件监听器的对象是一致的。然而body和framset元素有几个事件处理器,其对应的事件监听器却是被添加到Window对象上的(如果存在window对象)。

 

Event handlerEvent handler event type
onblurblur
onerrorerror
onfocusfocus
onloadload
onresizeresize
onscrollscroll

We call the set of the names of the event handlers listed in the first column of this table the Window-reflecting body element event handler set.

来源:片段链接

我们将上表首栏一系列事件处理器称为“window反馈body元素事件处理器”(译者注:直译很拗口。意思就是,在body上设置了事件处理器,但该处理器却会被添加到window对象上,而不是HTMLBodyElement对象上。)

因此在上述事件处理器内使用this关键词,其指代window对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值