<!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>
Run Code Online (Sandbox Code Playgroud)
我将this关键字放在按钮元素事件处理程序和body元素的另一个处理程序中,但是第二个this关键字引用了全局窗口对象。为什么?
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |