Chrome 似乎在 Meteor/Blaze 中定义了“事件"变量 [英] Chrome seems to define 'event' variable in Meteor/Blaze

查看:38
本文介绍了Chrome 似乎在 Meteor/Blaze 中定义了“事件"变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要使用 Chrome 进行开发.我刚刚发现了一个错误,以下代码段在 Chrome 中可以正常工作,但在 Firefox 中却没有.

Template.myTemplate.events({点击输入[类型=复选框]"(){让上下文 = event.target.dataset.context;InspireTheWorldWith.call({context});}});

我简直不敢相信自己的眼睛,但基本上从未在事件函数的参数中分配变量事件".然而,这在 Chrome 中没有任何问题.为了记录,它显然应该是:

Template.myTemplate.events({点击输入[类型=复选框]"(事件,模板){让上下文 = event.target.dataset.context;InspireTheWorldWith.call({context});}});

我想阅读更多相关信息以真正了解正在发生的事情,但我正在努力为谷歌找到合适的关键字".为了.

有什么建议吗?

为了记录,我使用的是 Meteor 1.4,babel-runtime@6.18

解决方案

与 Meteor 无关,与 Chrome 无关:Chrome 这样做是为了支持最初为 Internet Explorer 编写的代码;除了将事件传递给处理程序之外,它还将其设置为全局 event 变量.

微软在 IE5 中的新"事件处理(或者是 5.5?)使用了全局 event 对象和 attachEvent/removeEvent.然后 DOM2 出现(在 IE6 发布之后)并定义了 addEventListener/removeEventListener,其中事件处理程序接收事件作为参数.然后过了几年微软才支持 DOM2(在 IE9 中).

因此,当 Chrome 出现时,他们决定提供一个全局 event 对象,以提高与 IE 特定代码的兼容性.Mozilla 最初选择不这样做,但现在选择这样做;在我的回答中有更多内容.

你可以在这里看到它 —在 Chrome 上运行:

document.getElementById("target").addEventListener("click", function(e) {console.log("e === 事件?", e === 事件);}, false);

点击我

如您所见,全局event对象和处理程序接收到的事件对象是同一个对象.

I'm developing using mostly Chrome. I just discovered a bug, where the following snippet was working fine in Chrome, but not in Firefox.

Template.myTemplate.events({
  "click input[type=checkbox]"(){
    let context = event.target.dataset.context;
    InspireTheWorldWith.call({context});
  }
});

I could not quite believe my eyes, but essentially the variable 'event' was never assigned in the parameters of the event function. Yet this worked in Chrome without any issues. For the record, it clearly should be:

Template.myTemplate.events({
  "click input[type=checkbox]"(event, template){
    let context = event.target.dataset.context;
    InspireTheWorldWith.call({context});
  }
});

I'd like to read up more about this to really understand what is going on, but I'm struggling to find the approrpate 'keyword' to google. for.

Any suggestions?

For the record, I'm using Meteor 1.4, babel-runtime@6.18

解决方案

It's nothing to do with Meteor, just Chrome: Chrome does this to support code written originally for Internet Explorer; in addition to passing the event to the handler, it also sets it as a global event variable.

Microsoft's "new" event handling in IE5 (or was it 5.5?) used a global event object and attachEvent/removeEvent. Then DOM2 came along (after IE6 had been released) and defined addEventListener/removeEventListener where the event handler recieves the event as an argument. Then years passed before Microsoft supported DOM2 (in IE9).

So when Chrome was coming up, they made the decision to provide a global event object to increase compatibility with IE-specific code. Mozilla chose not to originally, but does now; more in my answer here.

You can see it here — run this on Chrome:

document.getElementById("target").addEventListener("click", function(e) {
  console.log("e === event? ", e === event);
}, false);

<div id="target">Click me</div>

As you can see, the global event object and the event object the handler receives are the same object.

这篇关于Chrome 似乎在 Meteor/Blaze 中定义了“事件"变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆