首页 > 编写的nextSibling函数不起作用

编写的nextSibling函数不起作用

1.问题描述:编写一个js函数,获取节点的下一个兄弟元素节点
2.代码如下:

function getNextElement(node) {

    var x = node.nextSibling;
    if (x.nodeType == 1) {
    document.write('ha!')
        return x;
    }
    if (x.nextSibling) {
        return getNextElement(x.nextSibling);
    }
    return null;
}

3.遇到问题:函数不起作用,调试打印x的nodeType属性都是undefined,
如果我把

        return getNextElement(x.nextSibling);      
          改成
        return getNextElement(node.nextSibling);

结果就好了 ,函数返回的结果都是正确的
求助为什么使用


x已经是node的nextSibling了,改成

var x = node

再试试


先调试看看函数传入的 node 是什么

【热门文章】
【热门文章】