【问题标题】:What is initEvents() and should I use it in 2018?什么是 initEvents(),我应该在 2018 年使用它吗?
【发布时间】:2018-01-09 23:00:25
【问题描述】:

我正在尝试理解另一个程序员的代码,并且有一个 initEvents() 方法。有些文章说这是一种过时的方法,但我不确定。 你可以帮帮我吗?这是什么?它真的过时了吗?

【问题讨论】:

标签: javascript events


【解决方案1】:

这确实是一个deprecated methodinit the event before dispatching,您应该改用Event constructor,它有一个用于事件初始化的第二个参数,然后根据您的需要调度事件。
像这样的:

// create a look event that bubbles up and cannot be canceled

var evt = new Event("look", {"bubbles":true, "cancelable":false});
document.dispatchEvent(evt);

// event can be dispatched from any element, not only the document
myDiv.dispatchEvent(evt);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 2011-01-18
    • 2011-08-18
    • 2017-03-21
    相关资源
    最近更新 更多