zoukankan      html  css  js  c++  java
  • 元素节点之间的操作

    一,通过子节点查询父节点:

    image

    二,

    image

    image

    function GetTwoEnd()
    {
        var myLinkItem=document.getElementById('linkedItem');
        var first=firstSibling(myLinkItem.parentNode);
        var last=lastSibling(myLinkItem.parentNode);
        alert(getTextContent(first));
        alert(getTextContent(last));

    }
    function lastSibling(node){
        var tempObj=node.parentNode.lastChild; //取最后一个node,这个node有可能是text
        while(tempObj.nodeType!=1&&tempObj.previousSibling!=null) //如果是text,并且它前面有个node,则取前面那个node,直至它为非text
        {
            tempObj=tempObj.previousSibling;
        }
        return (tempObj.nodeType==1)?tempObj:false;
    }

    function firstSibling(node)
    {
        var tempObj=node.parentNode.firstChild; //取第一个node,这个node有可能是text
        while(tempObj.nodeType!=1&&tempObj.nextSibling!=null) //如果是text,并且它有后面有个node,则取后面那个node,直至它为非text
        {
            tempObj=tempObj.nextSibling;
        }
        return(tempObj.nodeType==1)?tempObj:false;
    }

    function getTextContent(node)
    {
        return node.firstChild.nodeValue;
    }

    //window.onload=findElements;
    //window.onload=myDOMinspector;
    //window.onload=GetSlibling;
    window.onload=GetTwoEnd;

  • 相关阅读:
    迷宫寻找路径数
    136. 只出现一次的数字
    48. 旋转图像
    283. 移动零
    面试题 01.06. 字符串压缩
    位运算符
    367. 有效的完全平方数
    868. 二进制间距
    SpringAOP表达式
    Mybatis常见错误及纠错
  • 原文地址:https://www.cnblogs.com/vonk/p/3977359.html
Copyright © 2011-2022 走看看