为鼠标悬停时的 jquery 事件添加延迟

发布于 2024-09-18 10:53:54 字数 447 浏览 5 评论 0原文

我正在尝试为孩子的鼠标悬停事件添加一个简单的延迟,但遇到了困难。 (仍在学习!)

这使我能够在延迟后显示弹出窗口,但同时显示所有弹出窗口:

onmouseover='setTimeout(function() { $(\".skinnyPopup\").show(); }, 600)'

这可以仅显示我想要的弹出窗口,没有延迟:

onmouseover='$(this).children(\".skinnyPopup\").show()'

但组合不会:

onmouseover='setTimeout(function() { $(this).children(\".skinnyPopup\").show(); }, 600)'

任何帮助将不胜感激。谢谢!

I am trying to add a simple delay to a mouseover event of a child and having difficulties. (Still learning!)

This enables me to show the popup after a delay, but shows all of them simultaneously:

onmouseover='setTimeout(function() { $(\".skinnyPopup\").show(); }, 600)'

and this works to show only the popup I want with no delay:

onmouseover='$(this).children(\".skinnyPopup\").show()'

but the combination does not:

onmouseover='setTimeout(function() { $(this).children(\".skinnyPopup\").show(); }, 600)'

Any help would be appreciated. Thanks!

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

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

发布评论

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

评论(2

貪欢 2024-09-25 10:53:54

您需要在执行时定义 this 是什么,类似这样的东西可以工作:

setTimeout($.proxy(function() { $(this).children(".skinnyPopup").show(); }, this), 600)

或者只使用 .delay(),如下所示:

$(this).children(".skinnyPopup").delay(600).show(0);

以上两个都是快速修复,我建议您远离内联处理程序并查看 不引人注目方法(请参阅此答案,作者:Russ Cam 出于一些重要原因),例如:

$(function() {
  $('selector').mouseover(function() {
    $(this).children(".skinnyPopup").delay(600).show(0);
  });
});

You need to define what this is when it executes, something like this would work:

setTimeout($.proxy(function() { $(this).children(".skinnyPopup").show(); }, this), 600)

Or just use .delay(), like this:

$(this).children(".skinnyPopup").delay(600).show(0);

Both of the above are quick fixes, I suggest you move away from inline handlers and check out an unobtrusive method (see this answer by Russ Cam for some great reasons), for example:

$(function() {
  $('selector').mouseover(function() {
    $(this).children(".skinnyPopup").delay(600).show(0);
  });
});
£冰雨忧蓝° 2024-09-25 10:53:54

这是因为 this 绑定到全局上下文,而不是元素。请使用类似以下内容:

// put this in your document head -- replace element with a selector for the elements you want
$(function () {
    $(element).bind("mouseover", function () {
       var e = $(this);
       setTimeout(function () { e.children(".skinnyPopup").show(); }, 600);
    });
});

如果您坚持使用内联事件处理程序,则以下内容也应该有效:

onmouseover='var self = this; setTimeout(function() { $(self).children(\".skinnyPopup\").show(); }, 600)'

It's because this is bound to the global context, not the element. Use something like the following instead:

// put this in your document head -- replace element with a selector for the elements you want
$(function () {
    $(element).bind("mouseover", function () {
       var e = $(this);
       setTimeout(function () { e.children(".skinnyPopup").show(); }, 600);
    });
});

If you're adamant about inline event handlers, the following should also work:

onmouseover='var self = this; setTimeout(function() { $(self).children(\".skinnyPopup\").show(); }, 600)'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文