JavaScript實(shí)現(xiàn)添加、查找、刪除元素
代碼很簡單,這里就不多廢話了。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>測(cè)試文件</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="">中央情報(bào)局</a>:中央情報(bào)局</div> <div class="time">2014年5月4日21:56</div> <div class="replyButton"> <input type="button" id="submitButton" value="回復(fù)" onClick="showReplyArea(this)" /> </div> <div class="replyButton"> <input type="button" id="cancelButton" value="取消" onClick="hideReplyArea(this)" /> </div> </div> </div> <script> //顯示文本框的函數(shù) 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; } //隱藏文本框的函數(shù) 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; } //下面為調(diào)試時(shí)測(cè)試用函數(shù),做完后就沒用了,但也是很經(jīng)典的一段代碼,就留在這里了。 function showNode() { var i = 4; submitButton = document.getElementById('submitButton'); i = submitButton.parentNode.parentNode.getElementsByTagName("INPUT").item(1).value; alert(i); } </script> </body> </html>
示例二:
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 | $ )判斷后面是否有空格 兩個(gè)感嘆號(hào)為轉(zhuǎn)換為布爾值 以方便做判斷 }; 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方法是替換 }; }; };
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
- JS動(dòng)態(tài)創(chuàng)建元素的兩種方法
- js創(chuàng)建元素(節(jié)點(diǎn))示例
- JS中動(dòng)態(tài)創(chuàng)建元素的三種方法總結(jié)(推薦)
- 淺析JS動(dòng)態(tài)創(chuàng)建元素【兩種方法】
- JavaScript數(shù)組Array對(duì)象增加和刪除元素方法總結(jié)
- JS 添加刪除元素的實(shí)現(xiàn)代碼
- js中數(shù)組插入、刪除元素操作的方法
- javascript動(dòng)態(tài)創(chuàng)建及刪除元素的方法
- javascript刪除元素節(jié)點(diǎn)removeChild()用法實(shí)例
- javascript創(chuàng)建元素和刪除元素實(shí)例小結(jié)
相關(guān)文章
JS實(shí)現(xiàn)的系統(tǒng)調(diào)色板完整實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)的系統(tǒng)調(diào)色板,結(jié)合完整實(shí)例形式分析了js實(shí)現(xiàn)調(diào)色板效果的具體步驟與實(shí)現(xiàn)技巧,涉及js數(shù)值計(jì)算與頁面元素操作的方法,需要的朋友可以參考下2016-12-12layui禁用側(cè)邊導(dǎo)航欄點(diǎn)擊事件的解決方法
今天小編就為大家分享一篇layui禁用側(cè)邊導(dǎo)航欄點(diǎn)擊事件的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript倒計(jì)時(shí)定時(shí)器和間隔定時(shí)器使用詳解
這篇文章主要為大家介紹了JavaScript倒計(jì)時(shí)定時(shí)器和間隔定時(shí)器使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-05-05js刪除對(duì)象/數(shù)組中null、undefined、空對(duì)象及空數(shù)組方法示例
這篇文章主要給大家介紹了關(guān)于js刪除對(duì)象/數(shù)組中null、undefined、空對(duì)象及空數(shù)組的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編一起來看看吧2018-11-11三劍客:offset、client和scroll還傻傻分不清?
這篇文章主要給大家介紹了三劍客:offset,client和scroll的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12微信小程序?qū)崿F(xiàn)上傳word、txt、Excel、PPT等文件功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)上傳word、txt、Excel、PPT等文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05javascript實(shí)現(xiàn)隨時(shí)變化著的背景顏色
這篇文章主要介紹了javascript實(shí)現(xiàn)隨時(shí)變化著的背景顏色的方法,非常的簡單實(shí)用,有需要的小伙伴可以直接拿走。2015-04-04javascript中的Base64、UTF8編碼與解碼詳解
本文給大家介紹的是javascript中的Base64、UTF8編碼與解碼的函數(shù)源碼分享以及使用范例,十分實(shí)用,推薦給小伙伴們,希望大家能夠喜歡。2015-03-03JavaScript實(shí)現(xiàn)可動(dòng)的canvas環(huán)形進(jìn)度條
這篇文章主要介紹了如何利用JavaScript canvas繪制一個(gè)可以動(dòng)的環(huán)形進(jìn)度條。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試2022-02-02