最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS中使用DOM來控制HTML元素

 更新時間:2016年07月31日 15:54:08   作者:yanyuanyuan  
這篇文章主要介紹了JS中使用DOM來控制HTML元素的相關資料,需要的朋友可以參考下

1.getElementsByName():獲取name.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

例:

<p name="pn">hello</p>
   <p name="pn">hello</p>
   <p name="pn">hello</p>
   <script>
       function getName(){
                  var count=document.getElementsByName("pn");
                  alert(count.length);
                  var p=count[2];
                  p.innerHTML="world";
          }
   </script>

結果:界面打印出三個hello,并且伴有一個提示框“3”,當點擊確定后,界面顯示的內(nèi)容變?yōu)閔ello hello world

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~··

2.getElementsByTagName():獲取元素。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<p>hello</p>
   <p>hello</p>
   <p>hello</p>
   <script>
       function getName(){
                  var count=document.getElementsByTagName("p");
                  alert(count.length);
                  var p=count[2];
                  p.innerHTML="world";
          }
   </script>

結果:界面打印出三個hello,并且伴有一個提示框“3”,當點擊確定后,界面顯示的內(nèi)容變?yōu)閔ello hello world

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3.getAttribute():獲取元素屬性。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<a id="aid" title="得到a的標簽屬性"></a>
   <script>
           function getAttr1(){
               var anode=document.getElementById("aid");
               var attr=anode.getAttribute("id");
               alert("a的ID是:"+attr);
            }
           function getAttr2(){
              var anode=document.getElementById("aid");
              var attr=anode.getAttribute("title");
              alert("a的title內(nèi)容是:"+attr);
           }
            getAttr1();
            getAttr2();
  </script>

結果:彈出提示框“a的ID是:aid”.點擊確定后,彈出提示框“a的title內(nèi)容是:得到a的標簽屬性”。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4.setAttribute()設置元素屬性。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<a id="aid2">aid2</a> 
   <script>
        function setAttr(){
           var anode=document.getElementById("aid2");
           anode.setAttribute("title","動態(tài)設置a的title屬性");
           var attr=anode.getAttribute("title");
           alert("動態(tài)設置的title值為:"+attr);
       }
       setAttr();
   </script>

結果:彈出提示框“動態(tài)設置的title值為:動態(tài)設置a的title屬性”。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5.childNodes():訪問子節(jié)點。

~~~~~~~~~~~~~~~~~~~~~~~~~~··

<ul><li>1</li><li>2</li><li>3</li></ul>
   <script>
           function getChildNode(){
                var childnode=document.getElementsByTagName("ul")[0].childNodes;
                alert(childnode.length);
                alert(childnode[0].nodeType);
           }
          getChildNode();
  </script>

結果:界面打印出.1 .2 .3彈出對話框“3”,按確定后彈出“1”。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

6.parentNode():訪問父節(jié)點。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·

<div>
        <p id="pid"></p>
   </div>
   <script>
        function getParentNode(){
            var div=document.getElementById("pid");
            alert(div.parentNode.nodeName);
        }
       getParentNode();
  </script>

結果:彈出提示框:DIV.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

7.createElement():創(chuàng)建元素節(jié)點。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

例:

 <script>
       function createNode(){
             var body=document.body;
             var input=document.createElement("input");
             input.type="button";
             input.value="按鈕";
             body.appendChild(input);//插入節(jié)點
        }
         createNode();
 </script>

結果:出現(xiàn)一個按鈕。

~~~~~~~~~~~~~~~~~~~~~~~~~~~

8.createTextNode():創(chuàng)建文本節(jié)點。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

例:

    <script>
        function createNode(){
              var element = document.createElement("div");
              element.className = "message";
              var textNode = document.createTextNode("Hello world!");
              element.appendChild(textNode);
              document.body.appendChild(element);
        }
      createNode();
   </script>

代碼分析:這個例子創(chuàng)建了一個新<div>元素并為它指定了值為“message”的class特性。然后,又創(chuàng)建了一個文本節(jié)點,并將其添加到前面創(chuàng)建的元素中。最后一步,就是將這個元素添加到了文檔中的<body>元素中,這樣可以在瀏覽器中看到新創(chuàng)建的元素和文本節(jié)點了。

結果:頁面顯示hello world。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

9.insertBefore():插入節(jié)點。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 例

<div id="div">
          <p id="pid">p元素</p>
    </div>
    <script>
        function addNode(){
             var div=document.getElementById("div");
             var node=document.getElementById("pid");
             var newnode=document.createElement("p");
             newnode.innerHTML="動態(tài)插入一個p元素";
             div.insertBefore(newnode,node);
        }
        addNode();
    </script>

結果:界面打印出:動態(tài)插入一個p元素

                         p元素

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

10.removeChild():刪除節(jié)點。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·

 例

<div id="div">
          <p id="pid">p元素</p>
    </div>
    <script>
       function removeNode(){
               var div=document.getElementById("div");
               var p=div.removeChild(div.childNodes[1]);
        }
      removeNode();
   </script>

結果:界面什么也沒顯示。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

11.offsetHeight:網(wǎng)頁尺寸

12.scrollHeight:網(wǎng)頁尺寸

~~~~~~~~~~~~~~~~~~~~~~~~~~~·

例:

  <script>
      function getSize(){
          var width=document.documentElement.offsetWidth||document.body.offsetWidth;//解決兼容問題
          var height=document.documentElement.offsetHeight||document.body.offsetHeight;
          alert(width+","+height);
      }
      getSize();
 </script>

顯示結果:

這篇文章主要介紹了JS DOM 來控制HTML元素,文章內(nèi)容主要包括關于DOM,HTML等,文章來自網(wǎng)絡,請參考。

相關文章

最新評論

宜兰县| 安国市| 孟连| 垣曲县| 临颍县| 合阳县| 木兰县| 太白县| 绍兴县| 西宁市| 南京市| 比如县| 沙坪坝区| 固始县| 康马县| 抚顺县| 钟祥市| 营口市| 贵南县| 赞皇县| 云霄县| 唐河县| 邯郸县| 海伦市| 涡阳县| 昌乐县| 微山县| 安义县| 霍林郭勒市| 乐都县| 那坡县| 湟中县| 衡山县| 昌图县| 龙川县| 阳谷县| 河曲县| 桂阳县| 繁昌县| 沁源县| 武陟县|