# JS事件对象获取

179天前 · 代码 · 279次阅读

• DOM的event对象
• IE的event对象

### HTML事件处理

<button id="btn" onclick="handleClick(event)">Click me</button>
<script>
function handleClick(event) {
//...
}
</script>

### DOM0级，2级事件处理

let btn = document.querySelector('#btn');

let handleClick = function(e) {
let evt = e || window.event;
}

### 关于window.event的一些说明

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Note: This property can be fragile, in that there may be situations in which the returned Event is not the expected value. In addition, Window.event is not accurate for events dispatched within shadow trees.

let btn = document.querySelector('#btn');

let handleClick = function(event) {
console.log(event);
console.log(window.event);
console.log(window.event === event);
}

btn.addEventListener('click', handleClick, false);

127

1954

3

arknights!

107