第七章 動態建立HTML內容

來源:互聯網
上載者:User

標籤:

javascript也可以改變網頁的結構和內容

  • document.write()方法

可以方便快捷地把字串插入到文檔裡

document.write("<strong>hello world.</strong>");/*function insertP(text){    var str = "<p>";    str += text;    str +="</p>";    document.write(str);}insertP("hello world!");*/
  • innerHTML 屬性

該屬性可以用來讀、寫某給定元素裡的HTML內容。

<div id="test">    </div><script type="text/javascript">    window.onload = function(){        var testdiv = document.getElementById("test");        testdiv.innerHTML ="<p>這是一句話</p>"    }</script>
  • createElement()方法

建立一個元素節點。這個方法本身並無實際用處,需要把新建立出來的元素插入文檔才能達到實際目的。

document.creatElement(node.name)

 

  • appendChild()方法

讓新建立的節點成為文檔某個現有節點的一個子節點。

parent.appendChild(child)

var test = document.getElementById("test");var para = document.createElement("p");test.appendChild(para);            //document.getElementById("test").appendChild(document.createElement("p"));

 

  • creatTextNode()方法

建立一個文本節點。文法:document.creatTextNode(text)

var test = document.getElementById("test");var txt = document.createTextNode("hello world");test.appendChild(text);
  • insertBefore()方法

這個方法將把一個新 元素插入到一個現有元素的前面。想調用此方法,必須知道三件事:

-想插入的新元素

-想把新元素插入到哪個現有元素的前面

-這兩個元素共同的父元素

parentElement.insertBefore(newElement,targetElent)

 比如,把description插入到圖片列表imagegallery的前面。

var gallery = document.getElementById("imagegallery");      gallery.parentNode.insertBefor(description,gallery);

 

  • 編寫insertAfter()函數
/* 編寫insertAfter函數 */function insertAfter(newElement,targetElement){    var parent = targetElement.parentNode;    if (parent.lastChild == targetElement){        parent.appendChild(newElement);    }else{        parent.insertBefor(newElement,targetElement.nextSibling);    }}

 

 

第七章 動態建立HTML內容

聯繫我們

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