JS document對象簡單用法完整示例
本文實例講述了JS document對象簡單用法。分享給大家供大家參考,具體如下:
<html> <head> <title>js-document對象學(xué)習(xí)</title> <meta charset="UTF-8"/> <script type="text/javascript"> // 直接獲取對象 function testGetElementById(){ //通過id獲取對象 // var gby=window.document.getElementById(); //window可以省去不寫 var gby=document.getElementById("sid"); alert(gby); //輸出的返回值是標簽的類型[object HTMLInputElement] } function testGetElementsByName(){ //通過name獲取對象 var gbn=document.getElementsByName("umane"); alert(gbn); //輸出的返回值類型是[object NodeList]一個對象類型的數(shù)組 alert(gbn.length); //Nodelist的元素是節(jié)點元素,長度是節(jié)點的個數(shù)。每一個容器或標簽算是一個節(jié)點。 } function testGetElementsByTagName(){ //通過TagName(標簽)獲取對象 var gbt=document.getElementsByTagName("input"); alert(gbt); //輸出返回值類型是[object HTMLCollection]是一個對象元素的集合 alert(gbt.length); //其集合的數(shù)目是所有input個數(shù) } function testGetElementsByClassName(){ //通過class獲取對象 var gbc=document.getElementsByClassName("clname"); alert(gbc); //輸出返回值類型是[object HTMLCollection]是一個對象元素的集合 alert(gbc.length); //集合元素的長度是含有傳入類屬性的元素個數(shù)。 } // 間接獲取對象 function testParent(){ //利用父節(jié)點調(diào)用其節(jié)點對象 var showdiv=document.getElementById("showdiv"); var tchild=showdiv.childNodes; alert(tchild); //輸出返回值類型是[object NodeList]的一個list數(shù)組 alert(tchild.length); //返回子節(jié)點的長度 13,是由于在div中和text有換行符算一個子節(jié)點 } function testChild(){ //利用子節(jié)點調(diào)用其父節(jié)點 var showdiv=document.getElementById("child"); var tparent=showdiv.parentNode; alert(tparent); //輸出返回值類型是[object HTMLDivElement](其父節(jié)點的類型) } function testBorther(){ //利用子節(jié)點調(diào)用其父節(jié)點 var showdiv=document.getElementById("target"); var topBorther=showdiv.previousSibling; //輸出參考對象上面的元素 var lowBorther=showdiv.nextSibling //輸出參考元素的下面的元素 alert(topBorther+":::"+lowBorther); //輸出返回值類型是[object HTMLDivElement](其父節(jié)點的類型) } </script> <style type="text/css"> .clname{} #showdiv{ border: solid 2px cyan; width: 300px; height: 400px; } input[type=text]{ margin-left: 3px; } </style> </head> <body> <h3>js-document對象學(xué)習(xí)</h3> <h4>直接調(diào)用</h2> <input type="button" name="" id="sid" value="測試GetElementById" onclick="testGetElementById()" /> <input type="button" name="umane" id="" value="測試GetElementByName" onclick="testGetElementsByName()" /> <input type="button" name="" id="" value="測試GetElementsByTagNames" class="clname" onclick="testGetElementsByTagName()" /> <input type="button" name="" id="" value="測試GetElementsByClassName" class="clname" onclick="testGetElementsByClassName()" /> <hr /><br /> <h4>間接調(diào)用</h2> <input type="button" name="" id="" value="測試Parent" onclick="testParent()" /> <input type="button" name="" id="" value="測試 Child" onclick="testChild()" /> <input type="button" name="" id="" value="測試Borther" onclick="testBorther()" /> <div id="showdiv"> <input type="text" name="" id="parent" value="" /> <input type="text" name="" id="child" value="" /> <input type="text" name="" id="target" value="" /> <input type="text" name="" id="" value="" /> <input type="text" name="" id="" value="" /> <input type="text" name="" id="" value="" /> </div> </body> </html>
運行結(jié)果:
Document:
獲取HTML元素:
1:直接獲取方式:通過id 通過name屬性值 通過標簽名 通過class屬性值
2:間接獲取方式:父子關(guān)系 子父關(guān)系 兄弟關(guān)系
3:操作HTML元素對象的屬性
操作HTML元素對象的內(nèi)容和樣式
操作HTML的文檔結(jié)構(gòu)
document操作Form元素
document操作表格
document對象實現(xiàn)form表單校驗
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
詳解JavaScript基礎(chǔ)知識(JSON、Function對象、原型、引用類型)
這篇文章主要介紹了JavaScript基礎(chǔ)知識(JSON、Function對象、原型、引用類型)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-01詳解js中常規(guī)日期格式處理、月歷渲染和倒計時函數(shù)
大家在日常開發(fā)的時候經(jīng)常要用到日期格式的處理,下面這篇文章主要給大家介紹了js中常規(guī)日期格式處理、月歷渲染及倒計時函數(shù),有需要的朋友可以參考借鑒,下面來一起看看吧。2016-12-12JavaScript中立即執(zhí)行函數(shù)實例詳解
javascript和其他編程語言相比比較隨意,所以javascript代碼中充滿各種奇葩的寫法,有時霧里看花,當然,能理解各型各色的寫法也是對javascript語言特性更進一步的深入理解。這篇文章主要給大家介紹了關(guān)于JavaScript中立即執(zhí)行函數(shù)的相關(guān)資料,需要的朋友可以參考下。2017-11-11javascript數(shù)組使用調(diào)用方法匯總
javascript數(shù)組使用調(diào)用方法匯總...2007-12-12