JavaScript基於Dom操作實現尋找、修改HTML元素的內容及屬性的方法,javascriptdom

來源:互聯網
上載者:User

JavaScript基於Dom操作實現尋找、修改HTML元素的內容及屬性的方法,javascriptdom

本文執行個體講述了JavaScript基於Dom操作實現尋找、修改HTML元素的內容及屬性的方法。分享給大家供大家參考,具體如下:

當網頁被載入時,瀏覽器會建立頁面的文件物件模型(Document Object Model)。HTML DOM 模型被構造為對象的樹。

通過可程式化的物件模型,JavaScript 獲得了足夠的能力來建立動態 HTML。例如:改變HTML元素,改變HTML 屬性,改變CSS樣式,事件響應。

:

代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>Javascript HTML DOM</title><head> <style type="text/css">  body {background-color:#eeeeee} </style></head><body> <h3>(一)通過 id 尋找 HTML 元素</h3> <p id = "hw">Hello world!</p> <script>  x = document.getElementById("hw");  document.write('<p>id="hw"的段落的文本是:'+x.innerHTML+'</p>'); </script> <button onclick = "setCurrentTime()">將id="hw"的文字改為目前時間</button> <script>  function setCurrentTime(){   x = document.getElementById("hw");   x.innerHTML = Date()  } </script> <h3>(二)通過 標籤名 尋找 HTML 元素</h3> <div id = "mainDiv">  <p>This is a paragragph.</p>  <p>This is another paragragph.</p>  <p>Yes you're right! This is also paragragph.</p> </div> <script>  xx = document.getElementById("mainDiv");  tagContents = xx.getElementsByTagName("p");  document.write("<p>使用Javascript尋找id為mainDiv下的p標籤的內容</p>");  for(i=0;;i++){   var tag = tagContents[i]   if(tag!=null){    document.write("<p>"+tag.innerHTML+"</p>")   }else{    document.write("<p>共有"+i+"條內容</p>")    break;   }  } </script> <h3>(三)修改 HTML 的屬性</h3> <img id = "bol" src = "images/eg_bulboff.gif" width="70px" height="120px"/> <p><button onclick = "changeSrc()">改變圖片資源</button></p> <script>  function changeSrc(){   x = document.getElementById("bol");   if (x.src.match("eg_bulboff.gif")){    x.src = "images/eg_bulbon.gif"   }else{    x.src = "images/eg_bulboff.gif"   }  } </script> <h3>(四)修改 CSS 樣式</h3> <p>  <span id = "para_1">This is a test paragraph.</span>  <button onclick="changeTextColor()">改變文字顏色</button> </p> <p>  <span id = "para_2">This is another paragraph.  <button onclick="changeTextFont()">改變字型</button> </p> <p>  <span id = "para_3">This is HELLO WORLD.</span>  <button onclick="changeTextSize()">改變字型大小</button> </p> <p>  <button onclick="changeVisibility()">顯示/隱藏</button>  <span id = "para_4">樣本文字</span> </p> <script>  function changeTextColor(){   ele_1 = document.getElementById("para_1");   ele_1.style.color = "red";  }  function changeTextFont(){   ele_2 = document.getElementById("para_2");   ele_2.style.fontFamily = "Arial";  }  function changeTextSize(){   ele_3 = document.getElementById("para_3");   ele_3.style.fontSize = "larger";  }  document.getElementById("para_4").style.visibility = "visible"  function changeVisibility(){   ele_4 = document.getElementById("para_4");   if(ele_4.style.visibility.match("visible")){    ele_4.style.visibility = "hidden"   }else{    ele_4.style.visibility = "visible"   }  } </script></body></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.