JavaScript動態改變頁面元素,javascript動態網頁面

來源:互聯網
上載者:User

JavaScript動態改變頁面元素,javascript動態網頁面

可以通過JavaScript動態改變HTML中的元素

向HTML中添加元素

首先需要建立一個標籤,然後向該標籤中添加相應的內容,然後將建立好的標籤添加到相應的位置。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>測試文檔</title><script type="text/javascript">function add(){var element = document.createElement("p");var node = document.createTextNode("添加新段落");element.appendChild(node);x = document.getElementById("demo");x.appendChild(element);}</script></head><body><div id="demo"><p>這是第一段</p></div><input  type="button" value="按鈕" onclick="add()" /></body></html>
刪除HTML中的某個元素

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>測試文檔</title><script type="text/javascript">function deleteE(){var father = document.getElementById("demo");var child = document.getElementById("p1");father.removeChild(child);}</script></head><body><div id="demo"><p id="p1">這是第一段</p><p id="p2">這是第二段</p></div><input type="button" value="刪除" onclick="deleteE()" /></body></html>




javascript可以不可以動態更改網頁裡某個元素的id

<input type='text' id="a">
<input type='button' onclick="document.all('a').id='b'">
按鈕只能按一次,而且text的ID改變後在源檔案裡是看不出來的,但是你再次點擊按鈕的時候,就顯示沒有那個a的對象了
 
javascript是怎更改網頁上的元素內容的?

動態是指網頁中資料的動態性,比如新聞類網站,資料必須是動態方可即時更新

js更改網頁的元素內容,看你要改什麼內容了

如果是文字的話,首先你要在頁面放一個對象,如:
<span id=mytext></span>

//js

document.all["mytext"].innerText = "設定文字內容";
 

聯繫我們

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