为什么 jQuery 无法将鼠标悬停处理程序附加到 Flash 对象?

发布于 2024-11-29 10:34:52 字数 696 浏览 1 评论 0原文

由于某种原因,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2024-12-06 10:34:52

jQuery 不支持 上的 data() >。由于 jQuery 的事件处理程序堆栈依赖于 data() 来工作,因此在对象上调用 mouseover() 将会失败。

因此,只要您不使用 jQuery 的事件处理,您就会做得很好:

var banner = $('#content-banner');
banner.live ( 'mouseover', ... ); // works, becuse live hooks to document not to banner
banner[0].onmouseover = ... ; // works
banner[0].addEventListener('mouseover', ... , false); // also works

如果您深入研究代码,您会发现 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 on data() to work, thus calling mouseover() on an object would fail.

So, as long as you don't use jQuery's event handing you would do fine:

var banner = $('#content-banner');
banner.live ( 'mouseover', ... ); // works, becuse live hooks to document not to banner
banner[0].onmouseover = ... ; // works
banner[0].addEventListener('mouseover', ... , false); // also works

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.

暖伴 2024-12-06 10:34:52

Use jQuery live. Since the flash is initialized after the page is loaded so it is not able to find the element. Working demo


Update

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 use live which will attach the event on the root document but will work as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文