JS擷取文本節點

來源:互聯網
上載者: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             //擷取文本節點10             window.onload = function(){11                 12                 //文本節點一定是元素節點的子節點. 13                 14                 //1. 擷取文本節點所在的元素節點15                 var bjNode = document.getElementById("bj");16                 17                 //2. 通過 firstChild 定義為到文本節點18                 var bjTextNode = bjNode.firstChild;19                 20                 //3. 通過操作文本節點的 nodeValue 屬性來讀寫文本節點的值. 21                 alert(bjTextNode.nodeValue);22                 23                 bjTextNode.nodeValue = "尚矽谷";24                 25                 //alert(bjTextNode);26             }27             28         </script>29         30     </head>31     <body>32         <p>你喜歡哪個城市?</p>33         <ul id="city">34             <li id="bj" name="BeiJing">北京</li>35             <li>上海</li>36             <li>東京</li>37             <li>首爾</li>38         </ul>39         40         <br><br>41         <p>你喜歡哪款單機遊戲?</p>42         <ul id="game">43             <li id="rl">紅警</li>44             <li>實況</li>45             <li>極品飛車</li>46             <li>魔獸</li>47         </ul>48         49         <br><br>50         name: <input type="text" name="username" 51             id="name" value="atguigu"/>52     </body>53 </html>    

 

JS擷取文本節點

聯繫我們

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