Why is #39;event#39; available globally in Chrome but not FF?(为什么“事件在 Chrome 中全局可用,但在 FF 中不可用?)
问题描述
在解决另一个问题的答案时,出现了一个与 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 中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么“事件"在 Chrome 中全局可用,但在 FF 中不可用?


- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- addEventListener 在 IE 11 中不起作用 2022-01-01