如何使用jquery的实时功能来模拟mouseenter事件? [英] How should I emulate a mouseenter event using jquery's live functionality?

查看:137
本文介绍了如何使用jquery的实时功能来模拟mouseenter事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery对于mouseenter事件的极好的实时功能,但目前不受支持。下面是我的第一个解决方案,但似乎不是最佳的。建议?改进?

I want to use jQuery's great live functionality for a mouseenter event, but it's currently unsupported. Below is my first go at a solution, but it doesn't seem optimal. Suggestions? Improvements?

// mouseenter emulation
jQuery('.selector').live('mouseover',function (e) {
    // live sees all mouseover events within the selector
    // only concerned about events where the selector is the target
    if (this != e.target) return; 
    // examine relatedTarget's parents to see if target is a parent. 
    // if target is a parent, we're "leaving" not entering
    var entering = true;
    jQuery(e.relatedTarget).parents().each(function () {
            if (this == e.target) {
                entering = false;
                return false; // found; stop searching
            }
    });
    if (!entering) return;
    /*
     the rest of my code 
     */
});

我无法检查目标的孩子的相关目标b / c没有一个简单的方法来获得所有的孩子节点。
我无法直接检查,如果目标的父母有相关的目标作为父母,因此进入目标,b / c的鼠标悬停,它可能是从相邻的兄弟姐妹而不是父母进入。

I can't check target's children for relatedTarget b/c there's not an easy way to get all child nodes. I can't check directly if target's parents have relatedTarget as a parent and thus "entering" target, b/c for mouseover, it could be entering from an adjacent sibling and not a parent.

此解决方案可以正常工作。我已经测试了,似乎很好,但是如何改进呢?它也受益于DOM的布局。必须暴露部分选择器元素才能看到鼠标悬停事件,但是在我正在尝试的示例中很少出现问题。不过,如果有办法保证会被看到,那将是很好的。

This solution works ok. I've tested it, and it seems fine, but how could I improve it? It also suffers from how the DOM is laid out. Some part of the selector elements must be exposed to see the mouseover event, but that's rarely a problem in the examples that I'm trying this on. Still, if there's a way to guarantee that it will be seen, that would be nice.

我想我想知道我是否正在接近这个权利,如果不,更好吗?

I guess I want to know if I'm approaching this right, and if not, what's better?

推荐答案

现在已经有一段时间没有接受者,所以我假设没有什么更好的。

It's been a while now with no takers, so I'm assuming there's nothing better.

我现在在几个项目中使用这个项目,所以我将把它从未回答的问题堆中拿出来。

I'm using this in a couple projects now, so I'll take it out of the unanswered questions pile.

希望别人觉得有用,如果你发现一个错误或想出更好的东西,让我知道。

Hope others find it useful, and if you find a bug or come up with something better, let me know.

这篇关于如何使用jquery的实时功能来模拟mouseenter事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆