簡體   English   中英

如何將mouseover事件發送到父div?

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

我有一個Div,其中有一個文本輸入,如下所示:

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

我已經通過JQuery為Div鼠標懸停事件和mouseout事件分配了一個功能。 但是當我將鼠標移到文本輸入上時,它會調用mouseout事件,而它在DIV中。

如何解決這個問題呢? 我應該將活動發送給家長嗎? 怎么樣?

使用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
});

現場測試案例

.hover()實際上綁定了mouseentermouseleave事件,這些都是您正在尋找的事件。

我建議您使用.hover()而不是.mouseover().mouseout()這里是一個實時工作示例http://jsfiddle.net/DeUQY/

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

);

您需要使用幾個步驟來完成這項工作。

首先,創建父hover函數,它們將是enter()exit() 這些是使用hover()函數設置的。 然后創建兒童enterChild()exitChild()函數。 孩子們只需更換一個標志,讓您知道孩子是否正在盤旋,因此父母仍然被認為是徘徊。

無論你想在exit()函數中做什么,你都不能立即這樣做,因為事件按照GUI的正確順序到達,但這個特定情況的順序錯誤:

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

因此,當您exit()函數被調用,你可能會后立即進入孩子,如果你希望當兩個家長和孩子都退出,只是作用於處理一些exit()一定是錯的。 請注意,瀏覽器的編寫方式是,如果發生enter事件,則始終會發生退出事件。 唯一的例外可能是,如果您關閉選項卡/窗口,在這種情況下,它們可能會放棄發送更多事件。

因此,在parent exit()函數中,我們使用setTimeout()調用來進行異步調用,這將在子進程的enter()函數發生之后發生。 這意味着我們可以在那里設置一個標志並在異步函數中測試它。

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事件 如何交換<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> div和childs上的Angular 2 mouseover事件 無法刪除Div上的mouseover事件 如何在父div上捕獲事件? 如何在重疊的div下獲取元素的mouseover事件 如何在每個鼠標懸停事件中更改單個 div 的顏色? VueJS父鼠標懸停事件屏蔽子鼠標懸停事件 如果父級有邊框,當鼠標退出父級和子級時,如何避免來自父級元素的mouseover事件 子點擊事件觸發父鼠標懸停事件
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM