欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS中使用DOM來控制HTML元素

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

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>

結(jié)果:界面打印出三個hello,并且伴有一個提示框“3”,當(dāng)點擊確定后,界面顯示的內(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>

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

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

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

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

<a id="aid" title="得到a的標(biāo)簽屬性"></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>

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

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

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

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

結(jié)果:彈出提示框“動態(tài)設(shè)置的title值為:動態(tài)設(shè)置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>

結(jié)果:界面打印出.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>

結(jié)果:彈出提示框: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>

結(jié)果:出現(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é)點了。

結(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>

結(jié)果:界面打印出:動態(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>

結(jié)果:界面什么也沒顯示。

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

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>

顯示結(jié)果:

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

相關(guān)文章

  • setTimeout和setInterval的瀏覽器兼容性分析

    setTimeout和setInterval的瀏覽器兼容性分析

    setTimeout和setInterval的瀏覽器兼容性分析...
    2007-02-02
  • javascript設(shè)計模式之工廠模式

    javascript設(shè)計模式之工廠模式

    這篇文章主要為大家介紹了javascript工廠模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • 深入解析JavaScript中的數(shù)字對象與字符串對象

    深入解析JavaScript中的數(shù)字對象與字符串對象

    這篇文章主要介紹了JavaScript中的數(shù)字對象與字符串對象,是JavaScript入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-10-10
  • 淺談js閉包理解

    淺談js閉包理解

    這篇文章主要介紹了js閉包理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • javascript es6的常用語法你知道嗎

    javascript es6的常用語法你知道嗎

    這篇文章主要為大家介紹了javascriptes6的常用語法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • 如何用JavaScript定義一個類

    如何用JavaScript定義一個類

    其實Javascript中沒有類這個定義,但是有類這個概念。很多人都寫過這樣的代碼,就是一個關(guān)鍵字 function,然后定義一個方法名,方法名后緊跟一對括號。如果你在項目中寫過這樣的代碼,那么祝賀你,你可以不費(fèi)任何吹毛之力,就能一口氣讀完這篇文章了。
    2014-09-09
  • JSON的語法與規(guī)則詳解

    JSON的語法與規(guī)則詳解

    這篇文章主要介紹了JSON的語法與規(guī)則詳解,Web?API可以接收J(rèn)SON格式的數(shù)據(jù),也可以返回JSON格式的數(shù)據(jù)。在接收J(rèn)SON?數(shù)據(jù)時,需要使用相應(yīng)的庫或框架來解析JSON數(shù)據(jù),在返回JSON數(shù)據(jù)時,可以使用相應(yīng)的庫或框架將數(shù)據(jù)轉(zhuǎn)換為JSON格式
    2023-07-07
  • htm調(diào)用JS代碼

    htm調(diào)用JS代碼

    htm調(diào)用JS代碼...
    2007-03-03
  • javascript設(shè)計模式之命令模式

    javascript設(shè)計模式之命令模式

    這篇文章主要為大家介紹了javascript命令模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • 詳解JavaScript正則表達(dá)式中的global屬性的使用

    詳解JavaScript正則表達(dá)式中的global屬性的使用

    這篇文章主要介紹了詳解JavaScript正則表達(dá)式中的global屬性的使用,是JS學(xué)習(xí)進(jìn)階中的重要知識點,需要的朋友可以參考下
    2015-06-06

最新評論