DOM学习1

来源:互联网 发布:怎么申请淘宝客服介入 编辑:程序博客网 时间:2024/05/21 15:48

DOM属性:

  • nodeName 报告节点的名称(详见下述)。
  • nodeValue 提供节点的 “值”(详见后述)。
  • parentNode 返回节点的父节点。记住,每个元素、属性和文本都有一个父节点。
  • childNodes 是节点的孩子节点列表。对于 HTML,该列表仅对元素有意义,文本节点和属性节点都没有孩子。
  • firstChild 仅仅是 childNodes 列表中第一个节点的快捷方式。
  • lastChild 是另一种快捷方式,表示 childNodes 列表中的最后一个节点。
  • previousSibling 返回当前节点之前 的节点。换句话说,它返回当前节点的父节点的 childNodes 列表中位于该节点前面的那个节点(如果感到迷惑,重新读前面一句)。
  • nextSibling 类似于 previousSibling 属性,返回父节点的 childNodes 列表中的下一个节点。
  • attributes 仅用于元素节点,返回元素的属性列表

    Example:

     // These first two lines get the DOM tree for the current Web page,
        //   and then the <html> element for that DOM tree
        var myDocument = document;
        var htmlElement = myDocument.documentElement;
        // What's the name of the <html> element? "html"
        alert("The root element of the page is " + htmlElement.nodeName);
        // Look for the <head> element
        var headElement = htmlElement.getElementsByTagName("head")[0];
        if (headElement != null) {
          alert("We found the head element, named " + headElement.nodeName);
          // Print out the title of the page
          var titleElement = headElement.getElementsByTagName("title")[0];
          if (titleElement != null) {
            // The text will be the first child node of the <title> element
            var titleText = titleElement.firstChild;
            // We can get the text of the text node with nodeValue
            alert("The page title is '" + titleText.nodeValue + "'");
          }
          // After <head> is <body>
          var bodyElement = headElement.nextSibling;
          while (bodyElement.nodeName.toLowerCase() != "body") {
            bodyElement = bodyElement.nextSibling;
          }
          // We found the <body> element...
          // We'll do more when we know some methods on the nodes.
        }

    DOM方法:

  • insertBefore(newChild, referenceNode)newChild 节点插入到 referenceNode 之前。记住,应该对 newChild 的目标父节点调用该方法。
  • replaceChild(newChild, oldChild)newChild 节点替换 oldChild 节点。
  • removeChild(oldChild) 从运行该方法的节点中删除 oldChild 节点。
  • appendChild(newChild)newChild 添加到运行该函数的节点之中。newChild 被添加到目标节点孩子列表中的末端
  • hasChildNodes() 在调用该方法的节点有孩子时则返回 true,否则返回 false。
  • hasAttributes() 在调用该方法的节点有属性时则返回 true,否则返回 false
  • Example

    // These first two lines get the DOM tree for the current Web page,
        //   and then the <html> element for that DOM tree
        var myDocument = document;
        var htmlElement = myDocument.documentElement;
        // What's the name of the <html> element? "html"
        alert("The root element of the page is " + htmlElement.nodeName);
        // Look for the <head> element
        var headElement = htmlElement.getElementsByTagName("head")[0];
        if (headElement != null) {
          alert("We found the head element, named " + headElement.nodeName);
          // Print out the title of the page
          var titleElement = headElement.getElementsByTagName("title")[0];
          if (titleElement != null) {
            // The text will be the first child node of the <title> element
            var titleText = titleElement.firstChild;
            // We can get the text of the text node with nodeValue
            alert("The page title is '" + titleText.nodeValue + "'");
          }
          // After <head> is <body>
          var bodyElement = headElement.nextSibling;
          while (bodyElement.nodeName.toLowerCase() != "body") {
            bodyElement = bodyElement.nextSibling;
          }
          // We found the <body> element...
          // Remove all the top-level <img> elements in the body
          if (bodyElement.hasChildNodes()) {
            for (i=0; i<bodyElement.childNodes.length; i++) {
              var currentNode = bodyElement.childNodes[i];
              if (currentNode.nodeName.toLowerCase() == "img") {
                bodyElement.removeChild(currentNode);
              }
            }
          }
        }