1

不知道该怎么做,但基本上我写了一个工具提示插件,它删除了mouseout或上的工具提示mousedown

如果mousedown触发了一个事件,它将删除$that.parent()哪个很好,这会删除工具提示,但是如果用户还触发了 mouseout 事件(他们将触发事件,因为mouseoverandmouseout事件当前被链接),它将删除另一个 DOM 元素我不想要。所以基本上我想知道这是否可能:

$that.on('mouseover', function() {

    // If this event is triggered within the mouseover event, don't run the chained mouseout event
    $that.on('mousedown', function() {
        $that.parent().next().fadeOut(100).remove();
        return false;
    });
}).mouseout(function() {
  // If they clicked above, don't run this
    $that.parent().next().fadeOut(100).remove();
});​

据我所知,如果不使用全局变量,就很难访问clicked该事件内部的布尔集,mousedown例如:

$that.on('mouseover', function() {
    clicked = false;
    // If this event is triggered within the mouseover event, don't run the chained mouseout event
    $that.on('mousedown', function() {
        clicked = true;
        $that.parent().next().fadeOut(100).remove();
        return false;
    });
}).mouseout(function() {
    // If they clicked above, don't run this
    if (clicked) {
        $that.parent().next().fadeOut(100).remove();
    }
});​

关于如何优雅地构建它的任何想法?

编辑:中的元素$that.parent().next()只是<div class="js-tooltip"><span>Tooltip text</span></div>

但这应该无关紧要,因为我只想知道如果在不使用全局变量的情况下触发了该mouseover函数是否可以从该函数返回。mousedown

4

2 回答 2

1

你不需要mouseover.

$that.on('mouseleave mouseup', function(e) {
     if( e.type === 'mouseleave'){
         // do the mouseleave stuff
     }else{
         // do the mouseup stuff
     }
});​

正如你所说,如果元素是动态创建的,你应该使用:

$(document).on('mouseleave mouseup', $that, function(e) {

于 2012-05-02T20:32:16.403 回答
0

您是否考虑过像这样简单地使用类过滤器?

$that.parent().next('.js-tooltip').fadeOut(100).remove();

如果下一个不是工具提示,这根本不会做任何事情,据我所知,这应该可以解决问题。

使用您提出的方法,这样做会更清楚$that.clicked = false

或者怎么样(如果你想保持鼠标悬停 - 这只是为了展示原理;我不确定它是否会像这样工作):

$that.on('mouseover', function() {

    // If this event is triggered within the mouseover event, don't run the chained mouseout event
    $that.on('mousedown mouseout', function() {
        $that.parent().next().fadeOut(100).remove();
        $that.off('mousedown mouseout'); //prevent that happening again
        return false;
    });
});
于 2012-05-02T20:35:31.757 回答