为什么 jQuery 无法将鼠标悬停处理程序附加到 Flash 对象?
由于某种原因,jQuery (1.6.2) 无法将鼠标悬停处理程序附加到 Flash 对象。
有趣的是,getElementById().onmouseover = ...
按预期工作。
// fail
$('#content-banner').mouseover(function () {alert(1)});
// success
document.getElementById("content-banner").onmouseover = function (evt) { alert(3); };
有关详细信息,请参阅 jsFiddle 上的实时示例。
是什么阻止 jQuery 附加处理程序?
更新
一个快速修复方法是使用 ShankarSangoli。然而问题仍然存在。 为什么 jQuery 无法附加处理程序?
For some reason, jQuery (1.6.2) fails to attach a mouseover handler to a Flash object.
Amusingly, getElementById().onmouseover = ...
works as expected.
// fail
$('#content-banner').mouseover(function () {alert(1)});
// success
document.getElementById("content-banner").onmouseover = function (evt) { alert(3); };
See the live example at jsFiddle for details.
What prevents jQuery from attaching the handler?
Update
A quick fix would be to use live()
as suggested by ShankarSangoli. However the question still remains. Why does jQuery fail to attach the handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 不支持
因此,只要您不使用 jQuery 的事件处理,您就会做得很好:
如果您深入研究代码,您会发现
classid='clsid:D27CDB6E-AE6D-11cf-96B8 的对象有一个例外-444553540000'
,这是Flash,但是classid仅适用于IE。因此,简短的答案是避免在对象上使用 jQuery 事件。jQuery does not support
data()
on<applet>
,<embed>
, and<object>
. Since jQuery's event handler stack depends ondata()
to work, thus callingmouseover()
on an object would fail.So, as long as you don't use jQuery's event handing you would do fine:
If you dig into the code you can see that there is an exception for objects with
classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'
, which is Flash, however classid only works for IE. So, the short answer is avoid using jQuery events on objects.使用 jQuery
live
。由于 Flash 在页面加载后初始化,因此无法找到该元素。工作 演示更新
重申Sheepy的回答:jQuery 在 dom 元素的
data
中维护事件处理程序,但由于它不支持事件上的 data() 随附的。另一种方法是使用live
,它将事件附加到根文档上,但会按预期工作。Use jQuery
live
.Since the flash is initialized after the page is loaded so it is not able to find the element.Working demoUpdate
Reiterating Sheepy's answer: jQuery maintains the event handlers in
data
of the dom element but since it does not support data() on the events are never attached. The alternative is to uselive
which will attach the event on the root document but will work as expected.