我的 javascript 自定义选择的 onmouseout 问题

发布于 2024-10-15 20:18:51 字数 536 浏览 7 评论 0原文

我已经使用 javascript 创建了一个自定义选择,如下所述: http://v2.easy-designs .net/articles/replaceSelect/

关键思想是 select 是由包含两个(在我的例子中)的 组成的

  • 。我想要做的是添加一个 onmouseover 函数,该函数应如下所示:

    ulNode.onmouseout = function() {
          if (ulNode.className.indexOf('selectOpen') !- -1){
             选择我(li[0]);
          }
    }
    

    即,如果鼠标离开 ul,并且 ul 打开,则应选择第一个元素。这工作正常,但是当我在 li 之间移动鼠标时,即使我没有离开包含的 ul,这个函数也会被调用。知道为什么会发生这种情况吗?

    提前致谢!

  • I've created a custom select using javascript as described here: http://v2.easy-designs.net/articles/replaceSelect/

    The key idea is that the select is built of a containing two (in my case)

  • s. What I want to do is add an onmouseover function that should read something like:

    ulNode.onmouseout = function() {
          if (ulNode.className.indexOf('selectOpen') !- -1){
             selectMe(li[0]);
          }
    }
    

    i.e. if the mouse leaves the ul, and the ul is open, the first element should be selected. This works fine, but this function gets called when I move my mouse between li's, even though I haven't left the containing ul. Any ideas why that might happen?

    Thanks in advance!

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

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

    发布评论

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

    评论(2

    回梦 2024-10-22 20:18:51

    对于这种情况,mouseovermouseout 是错误的事件之王。当具有 mouseout 事件的元素内有其他元素时,它们会被频繁触发。你需要类似 mouseleavemouseenter

    jQuery 中的Mouseleave

    mouseover and mouseout are wrong king of events for this case. They are fired way too often when you have other elements inside the element that has mouseout event. You need something like mouseleave and mouseenter

    Mouseleave in jQuery

    孤独难免 2024-10-22 20:18:51

    尝试在 mouseout 事件之前实现一些延迟,如下所示:

    var hover_to = null;
    $("ul").mouseover(function() {
       window.clearTimeout(hover_to);
       // (A) your stuff...
    }).mouseout(function() {
       hover_to = window.setTimeout(function() {
          // (B) your stuff...
       },200);
    });
    

    这有望处理不需要的原子事件。
    请注意 (B) 中的范围。

    Try implementing some delay before the mouseout event like this:

    var hover_to = null;
    $("ul").mouseover(function() {
       window.clearTimeout(hover_to);
       // (A) your stuff...
    }).mouseout(function() {
       hover_to = window.setTimeout(function() {
          // (B) your stuff...
       },200);
    });
    

    This hopefully handles the nonwanted atomic events.
    Careful with the scope in (B).

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