将鼠标移出指定的div并保持原始div处于打开状态 [英] Mouseout on specified divs and keep original div open

查看:116
本文介绍了将鼠标移出指定的div并保持原始div处于打开状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用简单的英语来做到这一点:我有一个来自mouseover事件的open div,当我将鼠标从div中移出时,它会在鼠标移出时关闭,非常好.我需要的是,当我将鼠标移出时,如果我将鼠标移出到具有x或y类的div上,则openDiv将不会关闭,除class x或y类之外的其他任何div上的任何mouseout都将导致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之外也会使其变为粉红色,此时它应该变成灰色并隐藏.知道如何使其变成灰色并隐藏吗?

解决方案

$("#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(); 
    }
});

I'm trying to do this in plain english: I have an open div from a mouseover event, when I take the mouse out of the div it closes on mouse out, perfect. What I need is that when I mouseout, if I mouseout to a div with class x or class y, the openDiv will not close, any mouseout on any other div besides class x or class y, will cause the openDiv to close.

Here is what I have so far, but it doesn't work:

$("#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
    }
});

UPDATE: I need more help to select a working answer! jsfiddle.net/bUzPG/8 Hovering over class x,y,or z keeps it open, hovering over x or z turns the openDiv pink, but hovering outside the openDiv also turns it pink, when it should turn grey and hide it. Any idea how to make it turn grey and hide?

解决方案

$("#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(); 
    }
});

这篇关于将鼠标移出指定的div并保持原始div处于打开状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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