DIV CSS 佈局教程網

JavaScript 實現元素的添加、查找與刪除
編輯:關於JavaScript     

動態操作 DOM 是前端開發的看家本領,而添加、查找、刪除元素正是其中最基礎的三板斧。下面通過兩個實戰例子來演示。

示例一:一個評論回復組件。點擊「回復」時動態創建文本輸入框與表單,點擊「取消」時再將其移除:

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>測試文件</title> <style> .reply { width: 500px; height: 100%; overflow: hidden; background-color:#CCC; margin-top: 10px; } .infoArea { width: 380px; height: 100%; overflow: hidden; } .words { width: 380px; height: auto; margin: 5px 0px; float: left; font-size: 14px; } .time { margin-left: 10px; margin-bottom: 3px; width: 150px; height: 20px; line-height: 20px; float: left; font-family: 楷體; font-size: 14px; color: #999; } .replyButton { width: 60px; height: 20px; float: left; margin-bottom: 10px; } .replyButton input { font-size: 12px; } #cancelButton { visibility: hidden; } </style> </head> <body> <div class="reply"> <div class="infoArea"> <div class="words"><a href="">中央情報局</a>:中央情報局</div> <div class="time">2014年5月4日21:56</div> <div class="replyButton"> <input type="button" id="submitButton" value="回復" onClick="showReplyArea(this)" /> </div> <div class="replyButton"> <input type="button" id="cancelButton" value="取消" onClick="hideReplyArea(this)" /> </div> </div> </div> <script> //顯示文本框的函數 function showReplyArea(src) { inputText = document.createElement("DIV"); inputText.className = "inputText"; inputText.style.width = '100%'; inputText.style.height = '75px'; inputText.style.margin = '3px 0'; inputText.style.cssFloat = 'left'; var grandfather = src.parentNode.parentNode.parentNode; grandfather.appendChild(inputText); form1 = document.createElement("FORM"); form1.action = ""; form1.method = "post"; inputText.appendChild(form1); inputTextArea = document.createElement("TEXTAREA"); inputTextArea.style.width = '380px'; inputTextArea.style.height = '40px'; inputTextArea.style.marginLeft = '60px'; inputTextArea.style.marginTop = '3px'; inputTextArea.style.cssFloat = 'left'; inputTextArea.style.resize = 'none'; textSubmit = document.createElement("INPUT"); textSubmit.type = 'submit'; textSubmit.value = '提交'; textSubmit.style.marginLeft = '80px'; textSubmit.style.cssFloat = 'left'; form1.appendChild(inputTextArea); form1.appendChild(textSubmit); cancelButton = grandfather.getElementsByTagName("INPUT").item(1); cancelButton.style.visibility = 'visible'; submitButton = src; submitButton.disabled = true; } //隱藏文本框的函數 function hideReplyArea(src) { var grandfather = src.parentNode.parentNode.parentNode; var inputText = grandfather.getElementsByClassName('inputText').item(0); grandfather.removeChild(inputText); cancelButton = src; cancelButton.style.visibility = 'hidden'; submitButton = grandfather.getElementsByTagName("INPUT").item(0); submitButton.disabled = false; } //下面為調試時測試用函數,做完後就沒用了,但也是很經典的一段代碼,就留在這裡了。 function showNode() { var i = 4; submitButton = document.getElementById('submitButton'); i = submitButton.parentNode.parentNode.getElementsByTagName("INPUT").item(1).value; alert(i); } </script> </body> </html> 

示例二:封裝了 hasClass、addClass、removeClass 三個通用函數,用來判斷、添加和移除元素的 CSS 類名:

 window.onload = function(){ var gaga = document.getElementById( "gaga" ); addClass( gaga,"gaga1" ) addClass( gaga,"gaxx" ); removeClass( gaga,"gaga1" ) removeClass( gaga,"gaga" ) function hasClass( elements,cName ){ return !!elements.className.match( new RegExp( "(\\s|^)" + cName + "(\\s|$)") ); // ( \\s|^ ) 判斷前面是否有空格 (\\s | $ )判斷後面是否有空格 兩個感歎號為轉換為布爾值 以方便做判斷 }; function addClass( elements,cName ){ if( !hasClass( elements,cName ) ){ elements.className += " " + cName; }; }; function removeClass( elements,cName ){ if( hasClass( elements,cName ) ){ elements.className = elements.className.replace( new RegExp( "(\\s|^)" + cName + "(\\s|$)" )," " ); // replace方法是替換 }; }; };

這兩個例子分別展示了原生 JS 在動態增刪節點、操作元素屬性與類名方面的基本功,都是日常開發中經常派上用場的經典套路。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved