1

我试图用简单的英语做到这一点:我从鼠标悬停事件中有一个打开的 div,当我将鼠标从 div 中取出时,它会在鼠标移出时关闭,完美。我需要的是,当我鼠标移出时,如果我将鼠标移到具有 x 类或 y 类的 div 上,openDiv 将不会关闭,除了 x 类或 y 类之外的任何其他 div 上的任何鼠标移出都会导致 openDiv 关闭。

这是我到目前为止所拥有的,但它不起作用:

$("#openDiv").mouseout(function () {
    var $c = $(e.target); //div where mouse is
    if ($c.is('div.x') || ('div.y')) //if div where mouse is has class x or y
    {
        $("#openDiv").show(); //show or keep open from the mouseover event
    } else {
        $("#openDiv").hide(); //hide openDiv if mouse is anywhere outside openDiv or div with class x or y
    }
});

更新: 我需要更多帮助来选择有效的答案!jsfiddle.net/bUzPG/8 悬停在类 x、y 或 z 上使其保持打开状态,悬停在 x 或 z 上会使 openDiv 变为粉红色,但悬停在 openDiv 之外也会将其变为粉红色,此时它应该变为灰色并隐藏它。知道如何使它变灰并隐藏吗?

4

2 回答 2

1
$("#openDiv").mouseout(function (e) { //you forgot to add the event `e` element
    var $c = $(e.target);
    if ($c.is('div.x') || $c.is('div.y')) //you forgot $c.is on the second part
    {
        $("#openDiv").show(); 
    } else {
        $("#openDiv").hide(); 
    }
});
于 2011-06-27T19:35:24.897 回答
0

为什么不简单地保持简单的悬停逻辑(鼠标移出时隐藏),然后在鼠标悬停在 X 或 Y div 上时简单地重新显示它。

$('#openDiv').mouseout(function() {
    $(this).hide();
});

$('div.x').mousein(function() {
    $('#openDiv').show();
});

如果你让你的 $('div.x') 选择器有一个 ID 或至少一个不是整个 DOM 的上下文,我敢打赌,隐藏然后再次显示的“闪烁”甚至都不明显。

于 2011-06-27T19:32:27.007 回答