Question about event bubbling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john

    Question about event bubbling

    I1. notice that if you have an onclick event for both the document
    and for a particular element, the onclick for the element is called
    first and then it bubbles up to the one for the document. Is it
    possible to have an onclick event that is at the document level that
    is called first? I would like to intercept it there and return false
    so that it doesn't bubble down to the element level.

    2. On a completely unrelated topic, is it possible to change the
    cursor for a Select element (dropdown list)? I can change the cursor
    for all my other controls except for that one.

    Thanks in advance.
  • Martin Honnen

    #2
    Re: Question about event bubbling



    john wrote:
    [color=blue]
    > I1. notice that if you have an onclick event for both the document
    > and for a particular element, the onclick for the element is called
    > first and then it bubbles up to the one for the document. Is it
    > possible to have an onclick event that is at the document level that
    > is called first? I would like to intercept it there and return false
    > so that it doesn't bubble down to the element level.[/color]

    DOM Level 2 event compliant browsers like Mozilla, Netscape 6/7 or Opera
    7 allow you to set up capturing event listeners that are called while an
    event is propagated down the document tree and there you can stop the
    propagation if needed:

    <html lang="en">
    <head>
    <title>event capturing</title>
    <script type="text/javascript">
    function initEventListen ers () {
    if (document.addEv entListener) {
    document.addEve ntListener(
    'click',
    function (evt) {
    var srcElement = evt.target;
    while (srcElement.nod eType != 1) {
    srcElement = srcElement.pare ntNode;
    }
    if (srcElement.id == 'p1') {
    if (evt.stopPropag ation) {
    evt.stopPropaga tion();
    }
    }
    },
    true
    );
    }
    }
    initEventListen ers();
    </script>
    </head>
    <body>
    <p id="p1"
    onclick="alert( event.type + ' at ' + this.id);">
    Kibology for all.
    Click events shouldn't reach this paragraph.
    </p>
    <p id="p2"
    onclick="alert( event.type + ' at ' + this.id);">
    Kibology for all.
    Click events should reach this paragraph.
    </p>
    </body>
    </html>

    However IE4/5/6 only supports event bubbling up the document tree so
    there you can't use that approach.
    [color=blue]
    > 2. On a completely unrelated topic, is it possible to change the
    > cursor for a Select element (dropdown list)? I can change the cursor
    > for all my other controls except for that one.[/color]

    Try CSS, e.g.
    <select style="cursor: pointer;"
    some browsers might support that, IE probably not

    --

    Martin Honnen


    Comment

    Working...