简体   繁体   English

如何将mouseover事件发送到父div?

[英]How to send mouseover event to parent div?

I have a Div in which there is a text input, like this: 我有一个Div,其中有一个文本输入,如下所示:

<div id="parentDive" class="parent">
<input id="textbox"></input>
</div>

I have assigned a functionality to the Div mouseover event and mouseout event by means of JQuery. 我已经通过JQuery为Div鼠标悬停事件和mouseout事件分配了一个功能。 But when I move my mouse over the text input, it calls mouseout event, while it's in the DIV. 但是当我将鼠标移到文本输入上时,它会调用mouseout事件,而它在DIV中。

How to solve this problem? 如何解决这个问题呢? Should I send the event to the parent? 我应该将活动发送给家长吗? how? 怎么样?

Use the jQuery .hover() method instead of binding mouseover and mouseout : 使用jQuery .hover()方法而不是绑定mouseovermouseout

$("#parentDive").hover(function() {
    //mouse over parent div
}, function() {
    //mouse out of parent div
});

$("#textbox").hover(function() {
    //mouse over textbox
}, function() {
    //mouse out of textbox
});

Live test case . 现场测试案例

The .hover() is actually binding the mouseenter and mouseleave events, which are what you were looking for. .hover()实际上绑定了mouseentermouseleave事件,这些都是您正在寻找的事件。

I suggest to you to use .hover() not .mouseover() and .mouseout() here is a live working example http://jsfiddle.net/DeUQY/ 我建议您使用.hover()而不是.mouseover().mouseout()这里是一个实时工作示例http://jsfiddle.net/DeUQY/

$('.parent').hover(function(){
    alert('mouseenter');
},function(){
    alert('mouseleave');
}

);

You need to use a few steps to make that work. 您需要使用几个步骤来完成这项工作。

First, create the parent hover functions which would be enter() and exit() . 首先,创建父hover函数,它们将是enter()exit() These are setup using the hover() function. 这些是使用hover()函数设置的。 Then create the children enterChild() and exitChild() function. 然后创建儿童enterChild()exitChild()函数。 The children just change a flag that allows you to know whether a child is being hovered and thus the parent is still being considered to be hovered. 孩子们只需更换一个标志,让您知道孩子是否正在盘旋,因此父母仍然被认为是徘徊。

Whatever you want to do in the exit() function, you cannot do it immediately because the events arrive in the correct order for a GUI, but the wrong order for this specific case: 无论你想在exit()函数中做什么,你都不能立即这样做,因为事件按照GUI的正确顺序到达,但这个特定情况的顺序错误:

enter parent
exit parent
enter child
exit child
enter parent
exit parent

So when your exit() function gets called, you may be entering the child right after and if you want to process something when both the parent and child are exited, just acting on the exit() will surely be wrong. 因此,当您exit()函数被调用,你可能会后立即进入孩子,如果你希望当两个家长和孩子都退出,只是作用于处理一些exit()一定是错的。 Note that the browser is written in such a way that an exit event always happens if an enter event happened. 请注意,浏览器的编写方式是,如果发生enter事件,则始终会发生退出事件。 The only exception may be if you close the tab/window in which case they may forfeit sending more events. 唯一的例外可能是,如果您关闭选项卡/窗口,在这种情况下,它们可能会放弃发送更多事件。

So, in the parent exit() function we make use of a setTimeout() call to make an asynchronous call which will happen after the enter() function of a child happens. 因此,在parent exit()函数中,我们使用setTimeout()调用来进行异步调用,这将在子进程的enter()函数发生之后发生。 This means we can set a flag there and test it in the asynchronous function. 这意味着我们可以在那里设置一个标志并在异步函数中测试它。

MyNamespace = {};

MyNamespace.MyObject = function()
{
    var that = this;

    // setup parent
    jQuery(".parentDiv").hover(
        function()
        {
            that.enter_();
        },
        function()
        {
            that.exit_();
        });

    // setup children
    jQuery(".parentDiv .children").hover(
        function()
        {
            that.enterChild_();
        },
        function()
        {
            that.exitChild_();
        });
}

// couple variable members
MyNamespace.MyObject.prototype.parentEntered_ = false;
MyNamespace.MyObject.prototype.inChild_ = false;

MyNamespace.MyObject.prototype.enter_ = function()
{
    // WARNING: if the user goes really fast, this event may not
    //          happen, in that case the childEnter_() calls us
    //          so we use a flag to make sure we enter only once
    if(!this.parentEntered_)
    {
        this.parentEntered_ = true;

        ... do what you want to do when entering (parent) ...
    }
};

// NO PROTOTYPE, this is a static function (no 'this' either)
MyNamespace.MyObject.realExit_ = function(that) // static
{
    if(!that.inChild_)
    {
         ... do what you want to do when exiting (parent) ...

         that.parentEntered_ = false;
    }
};

MyNamespace.MyObject.prototype.exit_ = function()
{
    // need a timeout because otherwise the enter of a child
    // does not have the time to change inChild_ as expected
    setTimeout(MyNamespace.MyObject.realExit_(this), 0);
};

// detect when a child is entered
MyNamespace.MyObject.prototype.enterChild_ = function()
{
    this.inChild_ = true;
    this.enter_(); // in case child may be entered directly
};

// detect when a child is exited
MyNamespace.MyObject.prototype.exitChild_ = function()
{
    this.inChild_ = false;

    // We cannot really do this, although in case the child
    // is "exited directly" you will never get the call to
    // the 'exit_()' function; I'll leave as an exercise for
    // you in case you want it (i.e. use another setTimeout()
    // but save the ID and clearTimeout() if exit_() is not
    // necessary...)
    //this.exit_()
};

如何交换<div>使用“鼠标悬停”事件的元素?</div><div id="text_translate"><pre> let wrapperSt = document.querySelector(".wrapper"); for(i=0; i<100; i++){ let divGroup = document.createElement('div'); wrapperSt.append(divGroup); divGroup.className= 'pixel'; divGroup.textContent= ''; }</pre><p> 我使用循环创建了名为“pixel”的 div 元素,因为我需要数百个元素。 (<em>我会将它们用作可以改变颜色的小盒子</em>)</p><p> 但是,我希望这些框(“<em>像素</em>”div)变成棕色并维持( <em>style.backgroundColor ="brown";</em> )</p><p> 因此,我创建了另一个 div 来替换之前的 div(“ <em>pixel</em> ”)。</p><pre> let selectPx = document.getElementsByClassName("pixel"); selectPx.addEventListener("mouseover", function(){ let pxChange = createElement("div"); //This is where i got stuck! })</pre><p> 我无法完成我的代码,我发现它有点复杂,即使它可能非常简单。</p><p> 任何建议或信息都会非常有帮助。 谢谢你。</p></div> - How to swap <div> elements by using "mouseover" event?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在P和父div上都可以使用mouseover事件 - How do I get mouseover event to work on both p and parent div 如何交换<div>使用“鼠标悬停”事件的元素?</div><div id="text_translate"><pre> let wrapperSt = document.querySelector(".wrapper"); for(i=0; i<100; i++){ let divGroup = document.createElement('div'); wrapperSt.append(divGroup); divGroup.className= 'pixel'; divGroup.textContent= ''; }</pre><p> 我使用循环创建了名为“pixel”的 div 元素,因为我需要数百个元素。 (<em>我会将它们用作可以改变颜色的小盒子</em>)</p><p> 但是,我希望这些框(“<em>像素</em>”div)变成棕色并维持( <em>style.backgroundColor ="brown";</em> )</p><p> 因此,我创建了另一个 div 来替换之前的 div(“ <em>pixel</em> ”)。</p><pre> let selectPx = document.getElementsByClassName("pixel"); selectPx.addEventListener("mouseover", function(){ let pxChange = createElement("div"); //This is where i got stuck! })</pre><p> 我无法完成我的代码,我发现它有点复杂,即使它可能非常简单。</p><p> 任何建议或信息都会非常有帮助。 谢谢你。</p></div> - How to swap <div> elements by using "mouseover" event? div和childs上的Angular 2 mouseover事件 - Angular 2 mouseover event on div and childs 无法删除Div上的mouseover事件 - Cannot remove mouseover event on Div 如何在父div上捕获事件? - How to catch event on a parent div? 如何在重叠的div下获取元素的mouseover事件 - How to get mouseover event of an element under an overlayed div 如何在每个鼠标悬停事件中更改单个 div 的颜色? - How can I change the color of a single div with every mouseover event? VueJS父鼠标悬停事件屏蔽子鼠标悬停事件 - VueJS parent mouseover event masking child mouseover event 如果父级有边框,当鼠标退出父级和子级时,如何避免来自父级元素的mouseover事件 - How to avoid the mouseover event from the parent element when the mouse is exiting both parent and child if the parent has a border 子点击事件触发父鼠标悬停事件 - child click event firing parent mouseover event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM