IE和FF之间的下一个区别?

时间:2011-01-02 12:57:27

标签: javascript internet-explorer compatibility raphael

我刚刚在raphaeljs中编写了一个用于分层的javascript代码,它在FF上完美运行。 但它不适用于IE。对于任何对象,问题是IE返回null nextSibling

如何正确使用它,或者IE中是否有nextElementSibling调用?

这是我用来改变对象顺序的代码片段:

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>

这里是上面的。 html的一块工作

2 个答案:

答案 0 :(得分:1)

nextElementSibling属性仅在IE9中受支持,而在早期版本的IE中不受支持(您可以检查here

如果你想要你可以使用JQuery获得下一个兄弟,如下所示:

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

答案 1 :(得分:1)

尝试使用以下功能。它是下一个兄弟的跨浏览器代码段。

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