nextSibling & previousSibling under Mozilla

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pawe³ Ga³ecki

    #1

    nextSibling & previousSibling under Mozilla

    I wrote a following function just for testing purposes:
    <html>
    <head>
    <script>
    function show(obj) {
    alert(obj.previ ousSibling.rowI ndex)
    alert(obj.rowIn dex)
    alert(obj.nextS ibling.rowIndex )
    }
    </script>
    </head>
    <body>
    <table>
    <tr >
    <td></td><td></td></tr>
    <tr onclick="show(t his)">
    <td>1</td><td>somethin g</td></tr>
    <tr onclick="show(t his)">
    <td></td><td></td></tr>
    <tr onclick="show(t his)">
    <td>2</td><td>somethin g</td></tr>
    <tr onclick="show(t his)">
    <td></td><td></td></tr>
    <tr onclick="show(t his)">
    <td>3</td><td>somethin g</td></tr>
    <tr>
    <td></td><td></td></tr>
    </table>
    </form>
    </body>
    </html>

    Under MSIE it seems to work correctly, while when run under Mozilla 1.6 it
    returns "undefined" for previousSibling and nextSibling, is there any way
    of accessing neighbour rows from a "this" row under Mozilla?
  • Martin Honnen

    #2
    Re: nextSibling &amp; previousSibling under Mozilla



    Paweł Gałecki wrote:
    [color=blue]
    > I wrote a following function just for testing purposes:
    > <html>
    > <head>
    > <script>
    > function show(obj) {
    > alert(obj.previ ousSibling.rowI ndex)
    > alert(obj.rowIn dex)
    > alert(obj.nextS ibling.rowIndex )
    > }
    > </script>
    > </head>
    > <body>
    > <table>
    > <tr >
    > <td></td><td></td></tr>
    > <tr onclick="show(t his)">
    > <td>1</td><td>somethin g</td></tr>
    > <tr onclick="show(t his)">
    > <td></td><td></td></tr>
    > <tr onclick="show(t his)">
    > <td>2</td><td>somethin g</td></tr>
    > <tr onclick="show(t his)">
    > <td></td><td></td></tr>
    > <tr onclick="show(t his)">
    > <td>3</td><td>somethin g</td></tr>
    > <tr>
    > <td></td><td></td></tr>
    > </table>
    > </form>
    > </body>
    > </html>
    >
    > Under MSIE it seems to work correctly, while when run under Mozilla 1.6 it
    > returns "undefined" for previousSibling and nextSibling, is there any way
    > of accessing neighbour rows from a "this" row under Mozilla?[/color]

    No, you don't test for previousSibling or nextSibling, you are testing for
    previousSibling .rowIndex
    and that is undefined as previousSibling of your <tr> element is a text
    node and not an element node in Mozilla's document object model. This is
    a common misunderstandin g, IE throws out all whitespace between elements
    that authors use for formatting/indenting while Mozilla models them as
    text nodes.
    So you need to make sure that you look at the right node, for instance
    var sibling = obj.nextSibling ;
    while (sibling && sibling.nodeTyp e != 1) {
    sibling = sibling.nextSib ling;
    }
    if (sibling) {
    // access sibling.rowInde x
    }
    --

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

    Comment

    Working...