Portability of nextElementSibling/nextSibling

后端 未结 3 1491
长发绾君心
长发绾君心 2020-12-03 10:44

I\'m currently writing an accordion and running into the same problem as described in nextSibling difference between IE and FF? - specifically differences between Microsoft\

3条回答
  •  無奈伤痛
    2020-12-03 11:17

    nextSibling will see HTML code comments, so be sure to keep them out.

    Other than that you should be alright since you won't have any text nodes between your tr elements.

    The only other issue I could think of would be in Firefox 3 where nextElementSibling hadn't yet been implemented. So if you're supporting that browser, you'll need to manually emulate nextElementSibling. (Pretty sure they had it implemented in FF3.5 though.)

    You'll be safer to create a nextElementSibling() function:

    tr = tr.nextElementSibling || nextElementSibling(tr);
    
    function nextElementSibling( el ) {
        do { el = el.nextSibling } while ( el && el.nodeType !== 1 );
        return el;
    }
    

提交回复
热议问题