本文介绍了对象如何在JavaScript中实现Event接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此

它说我们可以使用一个实现Event接口的对象作为事件的监听器。



但是我找不到如何反对实现Event接口。正如我所尝试的那样:



  document.querySelector('#demo')。addEventListener ('click',{handleEvent:function(e){console.log(e)}},false) 
  #demo {height:200px;背景:#ccc;}  
 <!DOCTYPE html> < html lang =en>< head> < meta charset =UTF-8> <标题>文件< /标题>< /头><身体GT; < div id =demo>< / div>< / body>< / html>  



这个可以正确处理点击事件。



也许你可以给我一些关于此的文件。

解决方案

 回调接口EventListener {
void handleEvent(Event event);
};

描述于,链接到,再次在

引用回 EventListener 对象 handleEvent 是唯一命名的属性。

 回调接口EventListener {
void handleEvent(活动事件);
}






澄清

例如

  document.querySelector('#demo')。addEventListener('点击',{
abc:function(e){
console.log(e)
}
},false)

不会将事件发送到 abc 处理程序。虽然 handleEvent 标识符会调度事件。


In this .addEventListener in MDN

It says we can use an object that implements the Event interface as listener for the event.

But I can't find how can object to implement the Event interface. As I tried:

document.querySelector('#demo').addEventListener('click', {
  handleEvent: function (e) {
    console.log(e)
  }
}, false)
#demo {
  height: 200px;
  background: #ccc;
}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<div id="demo"></div>
</body>
</html>

This one can handle click event right.

Maybe you can give me some documents about this.

解决方案

At DOM

callback interface EventListener {
  void handleEvent(Event event);
};

is described at IDL Index, which links to 3.6. Interface EventTarget, mentioned again at 3.8. Dispatching events

which references back to the EventListener object where handleEvent is the only named property.

callback interface EventListener {
  void handleEvent(Event event);
}


Web IDL clarifies

For example

document.querySelector('#demo').addEventListener('click', {
  abc: function (e) {
    console.log(e)
  }
}, false)

does not dispatch event to abc handler. Though handleEvent identifier does dispatch event.

这篇关于对象如何在JavaScript中实现Event接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 03:17