jQuery AJAY 实时悬停事件?

发布于 2024-09-11 09:15:21 字数 549 浏览 3 评论 0原文

抱歉,我再次问这个问题,但我找不到解决方案。我知道那里有一些帖子,但对我来说似乎没有任何作用。我正在使用 jquery load() 方法加载网站的一部分。我会将以下规则应用于其中的所有链接:

$('.file a').live('click', function(e) {
    alert('click event firde');
});

此单击事件工作得非常好。 但是我也想在这些链接上有一个悬停事件:

$('.file a').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
    alert('why doesn't the hover event fire????');
 } else {
    // do something on mouseout
    }
});

我不知道为什么我的悬停事件不会触发?我可以只将一个带有 live 的事件处理程序应用于特定的选择器吗???

有什么想法吗?

sorry i'm asking this again, but I can't find a solution. I know there are a few posts out there, but for me nothing seems to work. I'm loading a part of a website with the jquery load() method. I'll apply the following rule to all links inside them:

$('.file a').live('click', function(e) {
    alert('click event firde');
});

this click event works just perfectly fine.
however i want to have a hover event on those links as well:

$('.file a').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
    alert('why doesn't the hover event fire????');
 } else {
    // do something on mouseout
    }
});

i have no idea why my hover event wont fire? can i only apply one event handler with live to a specific selector???

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

呆° 2024-09-18 09:15:21

您想要单独绑定事件,如下所示:

$('.file a').live('mouseenter', function() {
  //hover code
}).live('mouseleave', function() {
  //stopped hovering code
});

我们使用 mouseentermouseleave 因为这些是 .hover() 绑定到。

You want to bind the events separately, like this:

$('.file a').live('mouseenter', function() {
  //hover code
}).live('mouseleave', function() {
  //stopped hovering code
});

We're using mouseenter and mouseleave because these are the events that .hover() binds to.

雨后彩虹 2024-09-18 09:15:21

根据 jQuery 文档,“从 jQuery 1.4.1 .live() 开始,可以接受多个以空格分隔的事件...”。因此,只要您至少使用 v.1.4.1,就应该没问题。我发现的唯一问题是这一行:

alert('why doesn't the hover event fire????');

这应该是

alert("why doesn't the hover event fire????");

You had a single quote in a stringliteral by singlequotes 。这将导致该语句及其后的任何语句失败。

讽刺的是,Stack Overflow 的代码着色也在线上失败了。 :)

According to the jQuery documentation, "As of jQuery 1.4.1 .live() can accept multiple, space-separated events...". So as long as you are using at least v. 1.4.1 you should be fine. The only problem I found is this line:

alert('why doesn't the hover event fire????');

which should be

alert("why doesn't the hover event fire????");

You had a single quote in a string literal bound by single quotes. That would cause the statement and any after it to fail.

Ironically, Stack Overflow's code coloring fails on the line as well. :)

恍梦境° 2024-09-18 09:15:21

您确定至少使用 jquery 1.4.1 吗?任何低于该值的代码都将不起作用。

Are you sure you are using at least jquery 1.4.1? Anything lower and that code will not work.

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