JS節點的屬性: nodeType, nodeName, nodeValue

來源:互聯網
上載者:User

標籤:

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 2 <html> 3     <head> 4         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5         <title>Untitled Document</title> 6          7         <script type="text/javascript"> 8              9             //關於節點的屬性: nodeType, nodeName, nodeValue10             //在文檔中, 任何一個節點都有這 3 個屬性11             //而 id, name, value 是具體節點的屬性. 12             window.onload = function(){13                 14                 //1. 元素節點的這 3 個屬性15                 var bjNode = document.getElementById("bj");16                 alert(bjNode.nodeType); //元素節點: 117                 alert(bjNode.nodeName); //節點名: li18                 alert(bjNode.nodeValue); //元素節點沒有 nodeValue 屬性值: null19                 20                 //2. 屬性節點21                 var nameAttr = document.getElementById("name")22                                        .getAttributeNode("name");23                 alert(nameAttr.nodeType); //屬性節點: 224                 alert(nameAttr.nodeName); //屬性節點的節點名: 屬性名稱25                 alert(nameAttr.nodeValue); //屬性節點的 nodeValue 屬性值: 屬性值26                 27                 //3. 文本節點:28                 var textNode = bjNode.firstChild;    29                 alert(textNode.nodeType); //文本節點: 030                 alert(textNode.nodeName); //節點名: #text31                 alert(textNode.nodeValue); //文本節點的 nodeValue 屬性值: 文本值本身. 32             33                 //nodeType、nodeName 是唯讀.34                 //而 nodeValue 是可以被改變的。 35                 //以上三個屬性, 只有在文本節點中使用 nodeValue 讀寫文本值時使用最多. 36             }37             38         </script>39         40     </head>41     <body>42         <p>你喜歡哪個城市?</p>43         <ul id="city">44             <li id="bj" name="BeiJing">北京</li>45             <li>上海</li>46             <li>東京</li>47             <li>首爾</li>48         </ul>49         50         <br><br>51         <p>你喜歡哪款單機遊戲?</p>52         <ul id="game">53             <li id="rl">紅警</li>54             <li>實況</li>55             <li>極品飛車</li>56             <li>魔獸</li>57         </ul>58         59         <br><br>60         name: <input type="text" name="username" 61             id="name" value="atguigu"/>62     </body>63 </html>    

 

JS節點的屬性: nodeType, nodeName, nodeValue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.