在解决另一个问题的答案时,出现了一个与 event
对象相关的奇怪错误,该对象可在匿名函数中使用而无需传入.在 Chrome 中,以下工作正常,但 FF 抛出一个错误.
While working on an answer for another question, a strange bug came up related to the event
object being available in an anonymous function without being passed in. In Chrome the below works fine, but FF throws an error.
$(document).ready(function() {
$("#uspsSideboxTrackingClose").click(function() {
event.preventDefault();
console.log(event);
});
});
铬:
火狐:
ReferenceError: 事件未定义
ReferenceError: event is not defined
<小时>
众所周知
It is already known that
$("#uspsSideboxTrackingClose").click(function(event) { .. }
适用于两种浏览器.这里 是有问题的代码.这是 Chrome 或 FF 的错误,还是两种浏览器的预期行为?哪个浏览器合适?
works in both browsers. Here is the offending code. Is this a bug with Chrome or FF, or intended behavior by both browsers? Which browser is right?
在IE中,事件对象是一个全局对象,(它不传递给处理函数)但作为一个全局对象访问.您还可以将其作为窗口对象的属性来访问,例如 window.event
In IE, the event object was a global object, (which is not passed to the handler function) but accessed as a global object. You can also access it as a property of the window object like window.event
在 FF 和其他浏览器中,事件对象作为参数传递,因为在 FF 中没有名为 event
的全局属性,因此您会收到错误消息.
In in FF and other browsers the event object was passed as an argument, since in FF there is no global property called event
, you are getting the error message.
在 chrome 中,他们添加了对这两个功能的支持,因此您将获取事件对象作为全局引用和参数.
In chrome they have added support for both these features, so you will get the event object as a global reference and as an argument.
但是由于您使用的是 jQuery,因此 jQuery 会规范化这两种行为,并将始终将事件对象作为参数传递给事件处理程序.
But since you are using jQuery, jQuery normalizes these 2 behaviors and will always pass the event object as an argument to the event handler.
$(document).ready(function () {
$("#uspsSideboxTrackingClose").click(function (event) {
event.preventDefault();
console.log(event);
});
});
这篇关于为什么“事件"在 Chrome 中全局可用,但在 FF 中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!