nextSibling difference between IE and FF?

2019-07-25 18:34发布

问题:

I just wrote a javascript code for layering in raphaeljs it works perfectly on FF. But it doesn't on IE. The problem is IE returns null for nextSibling for any object.

How does one use it correctly, or is there a nextElementSibling call in IE?

Here is the code fragment I used to change the order of objects:

n = items[selected_item_id].nextSibling.id;
if (n != '') {
  items[selected_item_id].insertAfter(items[n]);
}

<div id="consarea">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%">
<desc>Created with Raphaël</desc>
<defs/>
<rect x="188" y="100" width="200" height="200" r="10" rx="10" ry="10" fill="#ee8515" stroke="none" style="opacity: 1;" opacity="1"/>
<rect x="253" y="158" width="50" height="50" r="0" rx="0" ry="0" fill="#0080ff" stroke="none" style="opacity: 1;" opacity="1" id="0"/>
<rect x="230" y="140" width="50" height="50" r="0" rx="0" ry="0" fill="#c03022" stroke="none" style="opacity: 1;" opacity="1" id="1"/></svg>

here it is above. the piece of the html im working on

回答1:

Try to use the following function. It is a cross browser code snippet for next sibling.

    function getNextElementSibling(CurrentElement) {
    if (CurrentElement.nextElementSibling) {
        return CurrentElement.nextElementSibling
    } else {
        do {
            CurrentElement = CurrentElement.nextSibling;
        } while (CurrentElement && CurrentElement.nodeType !== 1);
        return CurrentElement;
    }
}


回答2:

the nextElementSibling property is only supported in IE9 and not in previous versions of IE (you can check it here .

if you want you can get the next sibling using JQuery as follows:

var sibling = $('#' + selected_item_id).next();
alert(sibling.attr('id'));