国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript基于Dom操作實(shí)現(xiàn)查找、修改HTML元素的內(nèi)容及屬性的方法

2019-11-19 17:51:24
字體:
供稿:網(wǎng)友

本文實(shí)例講述了JavaScript基于Dom操作實(shí)現(xiàn)查找、修改HTML元素的內(nèi)容及屬性的方法。分享給大家供大家參考,具體如下:

當(dāng)網(wǎng)頁被加載時,瀏覽器會創(chuàng)建頁面的文檔對象模型(Document Object Model)。HTML DOM 模型被構(gòu)造為對象的樹。

通過可編程的對象模型,JavaScript 獲得了足夠的能力來創(chuàng)建動態(tài)的 HTML。例如:改變HTML元素,改變HTML屬性,改變CSS樣式,事件響應(yīng)。

效果圖:

代碼:

<!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"的文字改為當(dāng)前時間</button> <script>  function setCurrentTime(){   x = document.getElementById("hw");   x.innerHTML = Date()  } </script> <h3>(二)通過 標(biāo)簽名 查找 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標(biāo)簽的內(nèi)容</p>");  for(i=0;;i++){   var tag = tagContents[i]   if(tag!=null){    document.write("<p>"+tag.innerHTML+"</p>")   }else{    document.write("<p>共有"+i+"條內(nè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>

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript操作DOM技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 田东县| 铁岭市| 绵竹市| 兴城市| 靖宇县| 额敏县| 普宁市| 东兴市| 高碑店市| 鸡泽县| 宁德市| 兰西县| 九龙城区| 永清县| 台中县| 高邑县| 肇源县| 郎溪县| 互助| 蓝田县| 体育| 揭东县| 朝阳市| 龙游县| 临夏县| 屏南县| 黄石市| 延长县| 万山特区| 黑龙江省| 禹城市| 白朗县| 桂平市| 阜新市| 克山县| 揭西县| 红河县| 乌恰县| 栾城县| 库伦旗| 界首市|