JS DOM操作

來源:互聯網
上載者:User

標籤:header   href   parent   XML   script   title   使用   get   last   

Dom 

  Dom:文件物件模型(Document Object Model,簡稱DOM)是一種用於HTML和XML文檔的編程介面。它給文檔提供了一種結構化的表示方法,可以改變文檔的內容和呈現方式。

  1、document 

      它指的是HTML(顯示資料)或XML標籤(傳輸資料)

      例如:<body></body> <div></div><p></p> 這樣的標籤。

  2、object 對象  

    註:使用js操作html就需要先將html結果轉成js對象。
    轉成對象的四種形式
      (1)標籤名【多個】
        var objs = document.getElementsByTagName(“div”) //擷取文檔中的所有div
      (2)id【1個】//若有多個重複id預設擷取第一個
        var obj = document.getElementById("one");
      (3) name 【多個】
        var objs = document.getElementsByName("two");

      (4) class name【多個】
        var objs = document.getElementsByClassName("three");    

    <!-- 擷取對象 --><div></div> <div id="one">one</div> <div name="two">two</div> <div class="three">three</div><script>var obj1 = document.getElementsByTagName("div");var obj2 = document.getElementById("one");var obj3 = document.getElementsByName("two");var obj4 = document.getElementsByClassName("three");console.log(obj1);console.log(obj2);console.log(obj3);console.log(obj4);</script>

    轉換成對象便可進行3種操作
      (1)操作屬性
        對象名.屬性名稱 = “值”
      (2)修改內容
        innerText //修改常值內容
        innerHTMl //修改html
        也可以使用對象名.innerText 擷取文本值
        註:表單(input text area)取值時不可以使用innerText得使用value
      (3)修改樣式
        (1)對象名.style.css樣式名 = “值”
        (2)使用對象名. className = “值” 添加新的class名來修改樣式
            使用對象名. className+ = “ ” + “值” 添加新的class名來修改樣式

   <a id = "link" href="http://www.baidu.com" target = "_blank" title = "this is title">test </a>     <script>        /*修改屬性*/        var link = document.getElementById("link");        link.href = "http://www.hteis.cn";        link.title = "new title"        /*修改內容*/        link.innerText="恒泰能聯";   //修改常值內容        link.innerText="<b>恒泰能聯</b>";        link.innerHTML = "<h1>hteis</h1>"  //修改html        /*修改樣式*/        link.style.color ="red";        link.className = "test";        link.className += " " + "test1";
  </script>

3、Modal

  (1)將文檔想像成一個倒樹,每一部分(元素,內容,屬性,注釋)都是節點

  

  (2)只要找到一個節點按照關係就可以找到所有的節點
     父節點 :parentNode
     子節點(第一個,最後一個):childNodes firstNode lastNode
     兄弟節點(上一個(左邊),下一個(右邊)): previoussibling nextsibling
  (3)節點有
    節點類型nodeType
    節點名nodeName
    節點值nodeValue

<!-- modal --><div><b>eddd</b><p id="pp">ff<a href="#">agf</a><span>fdigig</span></p><button onclick="myFunction()">試一下</button><input type ="text" value ="abg" /></div><script>/*modal*/var pp = document.getElementById("pp");console.log(pp);console.log(pp.parentNode.nodeName);console.log(pp.previousSibling.previousSibling);console.log(pp.nextSibling.nextSibling);console.log(pp.childNodes);console.log(pp.firstChild.nodeType);//節點類型console.log(pp.firstChild.nodeValue);//節點值console.log(pp.lastChild.nodeName);//節點名function myFunction()   {var c=document.getElementsByTagName("button")[0];console.log(c.childNodes[0].nodeValue);}</script>

 

 若有錯誤歡迎留言指正


BlancheWang 
出處:http://www.cnblogs.com/hhw3
本文著作權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連。

 

JS DOM操作

聯繫我們

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