nextSibling difference between IE and FF?

冷暖自知 提交于 2019-12-11 05:55:11

问题


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'));


来源:https://stackoverflow.com/questions/4578455/nextsibling-difference-between-ie-and-ff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!