event.stopPropagation() does not work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • delerious@no.spam.com

    #1

    event.stopPropagation() does not work

    I have a DIV that contains some links. I have an onmouseout event handler on
    the DIV, and I want it triggered only when the mouse leaves the DIV. Since
    there are Anchors in the DIV, onmouseout events will be generated when the
    mouse moves from one link to another, and those events will bubble up to the
    DIV. According to all the documentation that I have read, I should be able to
    prevent that from happening by having an onmouseout event handler for each of
    the anchors, and calling event.stopPropa gation in the event handler. But it
    is not working in Mozilla and Opera, the onmouseout still bubbles up from the
    anchor to the DIV. I have it working in IE, but that is because IE does not
    support stopPropagation (), and instead uses window.event.ca ncelBubble=true ,
    which Mozilla and Opera do not support.

    Does anyone have any clue why stopPropagation () isn't working in Mozilla and
    Opera?

    You can see the problem in action here:


    Here is my HTML code:

    <BODY onload="init()" >
    <DIV id="submenu" onmouseout="ale rt('Mouse has just left the DIV')">
    <A href="fakepage. html">LINK 1</A>
    <A href="fakepage. html">LINK 2</A>
    <A href="fakepage. html">LINK 3</A>
    <A href="fakepage. html">LINK 4</A>
    <A href="fakepage. html">LINK 5</A>
    </DIV>
    </BODY>


    Here is my Javascript code:

    function init() {
    var submenu = null;
    if (document.getEl ementById)
    submenu = document.getEle mentById('subme nu');

    for (var i=0; i<submenu.child Nodes.length; i++) {
    if (submenu.childN odes[i].addEventListen er) {
    // this code is for Mozilla and Opera
    submenu.childNo des[i].addEventListen er('mouseout',
    disableEventPro pagation, false);
    }
    else if (submenu.childN odes[i].attachEvent) {
    // this code is for IE
    submenu.childNo des[i].attachEvent('o nmouseout',
    disableEventPro pagation);
    }
    }
    }

    function disableEventPro pagation(event) {
    if (event.stopProp agation) {
    // this code is for Mozilla and Opera
    event.stopPropa gation();
    }
    else if (window.event) {
    // this code is for IE
    window.event.ca ncelBubble = true;
    }
    }


    Here is the stylesheet I am using:

    DIV {
    background-color: red;
    left: 10px;
    top: 10px;
    position: absolute;
    }

    A {
    background-color: blue;
    color: white;
    display: block;
    margin: 0 0 5px 0;
    text-decoration: none;
    }

    A:hover {
    background-color: cyan;
    }

  • Martin Honnen

    #2
    Re: event.stopPropa gation() does not work



    delerious@no.sp am.com wrote:
    [color=blue]
    > I have a DIV that contains some links. I have an onmouseout event handler on
    > the DIV, and I want it triggered only when the mouse leaves the DIV. Since
    > there are Anchors in the DIV, onmouseout events will be generated when the
    > mouse moves from one link to another, and those events will bubble up to the
    > DIV. According to all the documentation that I have read, I should be able to
    > prevent that from happening by having an onmouseout event handler for each of
    > the anchors, and calling event.stopPropa gation in the event handler. But it
    > is not working in Mozilla and Opera, the onmouseout still bubbles up from the
    > anchor to the DIV. I have it working in IE, but that is because IE does not
    > support stopPropagation (), and instead uses window.event.ca ncelBubble=true ,
    > which Mozilla and Opera do not support.[/color]

    See
    http://www.faqts.com/knowledge_base/...d/1606/fid/145
    for a solution, there is no need to set up event handlers for all those
    descendant elements and to try to stop events from propagating, simply
    use event.relatedTa rget to distinguish a mouse leaving the element from
    the mouse leaving a child element.




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Martin Honnen

      #3
      Re: event.stopPropa gation() does not work



      delerious@no.sp am.com wrote:
      [color=blue]
      > I have a DIV that contains some links. I have an onmouseout event handler on
      > the DIV, and I want it triggered only when the mouse leaves the DIV. Since
      > there are Anchors in the DIV, onmouseout events will be generated when the
      > mouse moves from one link to another, and those events will bubble up to the
      > DIV. According to all the documentation that I have read, I should be able to
      > prevent that from happening by having an onmouseout event handler for each of
      > the anchors, and calling event.stopPropa gation in the event handler. But it
      > is not working in Mozilla and Opera, the onmouseout still bubbles up from the
      > anchor to the DIV. I have it working in IE, but that is because IE does not
      > support stopPropagation (), and instead uses window.event.ca ncelBubble=true ,
      > which Mozilla and Opera do not support.
      >
      > Does anyone have any clue why stopPropagation () isn't working in Mozilla and
      > Opera?
      >
      > You can see the problem in action here:
      > http://home.comcast.net/~delerious1/index4.html[/color]

      Well, write some code to inspect what is happening, for instance

      javascript: var el = document.getEle mentById('subme nu'); el.onmouseout =
      function (evt) { alert(evt.type + ' for target ' + evt.target + ' with
      relatedTarget ' + evt.relatedTarg et); }; void 0

      then you will see that mouseout events are fired on the div when the
      mouse moves over a child element. The mouse can always only be over one
      node so when the mouse has entered the div but then enters a child
      element or node of the div then mouseout is fired on the div.
      For an approach to handle the mouse leaving the div see my earlier posting.

      --

      Martin Honnen
      http://JavaScript.FAQTs.com/

      Comment

      • delerious@no.spam.com

        #4
        Re: event.stopPropa gation() does not work

        On Thu, 11 Dec 2003 12:03:19 +0100, Martin Honnen <mahotrash@yaho o.de> wrote:[color=blue]
        >Well, write some code to inspect what is happening, for instance
        >
        >javascript: var el = document.getEle mentById('subme nu'); el.onmouseout =
        >function (evt) { alert(evt.type + ' for target ' + evt.target + ' with
        >relatedTarge t ' + evt.relatedTarg et); }; void 0
        >
        >then you will see that mouseout events are fired on the div when the
        >mouse moves over a child element. The mouse can always only be over one
        >node so when the mouse has entered the div but then enters a child
        >element or node of the div then mouseout is fired on the div.
        >For an approach to handle the mouse leaving the div see my earlier posting.[/color]

        Thanks for the replies. This will surely help me out.

        Comment

        Working...