javascript學習7——javascript DOM(上)

來源:互聯網
上載者:User

什麼是DOM          DOM不是javascript,DOM是文檔。          DOM是一組用來描述指令碼怎樣與結構化文檔進行互動和訪問的web標準。          DOM定義了一系列對象、方法和屬性,用於訪問、操縱和建立文檔中的內容、結構、樣式以及行為。繼承在DOM中的作用           當瀏覽器解析完某文檔檔案後,每個結點並非就是一個簡單的Element對象的執行個體,而是繼承了很多東西的Element對象的擴充。
核心Node對象 Node結點名稱、類型和值          DOM2中規定的每種核心對象   節點類型對應的nodeName值 nodeValue屬性只適用於少數DOM對象,尤其是Attr、ProcessingInstructions、Comments、Text和CDATASection。除此之外,其它所有對象的nodeValue屬性都將返回null。 DOM2核心規範中規定的  每種nodeType預期的nodeValue值: nodeType:我們之前看到的nodeType,它會包含與下面表中某個命名常量對應的一個整型數值。表中列出的這些常量表示的是DOM核心對象,因此可以根據此確定派生某個節點的DOM核心對象的類型。DOM核心對象的nodeType常量: Node節點相互引用及屬性             節點的屬性:              alert(baidu.attributes.getNamedItem('href').nodeValue);(有來擷取某個節點的單個屬性) 下面是測試代碼:[html]  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title>無標題文檔</title>    <script type="text/javascript">      function test()      {          var baidu=document.getElementById('baiduId');          var div=document.getElementById('divId');                    //測試一下a標籤的ndoeName          //alert(baidu.nodeName);                    //測試一下斷行符號  現在IE和firefox都把斷行符號看與一個文檔結點了          //alert(div.childNodes[0].nodeName);                    //測試一下element結點的nodeValue          //alert(baidu.nodeValue);                    //測試a標籤子結點的nodeValue          //alert(baidu.childNodes[0].nodeValue);                    //測試注釋結點的nodeValue          //alert(div.childNodes[1].nodeValue);                    //測試 文本結點的 nodeType   Node.TEXT_NODE  IE和firefox都可以用了。          //if(div.childNodes[0].nodeType==Node.TEXT_NODE)          //{              //alert(div.childNodes[1].nodeValue);              //}                        //測試childnodes  //      var divChildNodes=document.getElementById('divId').childNodes;      //  for(var i=0;i<divChildNodes.length;i++)          //{              //alert(divChildNodes[i].nodeName);              //alert(divChildNodes[i].nodeValue);              //}          //}                              //attributes屬性。          //for(var i=0;i<baidu.attributes.length;i++)  //      {  //          //這兩個都可以  //          //alert(baidu.attributes.item(i).nodeName);  //          alert(baidu.attributes[i].nodeName);  //          };            alert(baidu.attributes.getNamedItem('href').nodeValue);          }      window.onload=test;  </script>    </head>    <body>      <div id="divId">          <!--這是注釋-->          <ul id="ulId">              <li id="baiduLiId">                  <a href="http://www.baidu.com/" id="baiduId">百度</a>              </li>              <li>                  <a href="http://www.google.com.hk/" title="google" id="googleId">google</a>              </li>              <li>                  <a href="http://www.sogou.com/" id="sogouId">sogou</a>              </li>          </ul>      </div>  </body>  </html>          節點的owerDocument屬性檢測節點的屬性:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.