JavaScript實(shí)現(xiàn)添加、查找、刪除元素
更新時(shí)間:2015年07月02日 11:15:45 投稿:hebedich
這篇文章主要匯總介紹了JavaScript實(shí)現(xiàn)添加、查找、刪除元素的方法,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。
代碼很簡(jiǎ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ù),做完后就沒(méi)用了,但也是很經(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方法是替換
};
};
};
以上所述就是本文的全部?jī)?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)文章
JavaScript中window和document用法詳解
這篇文章主要介紹了JavaScript中window和document用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
原生JS實(shí)現(xiàn)的碰撞檢測(cè)功能示例
這篇文章主要介紹了原生JS實(shí)現(xiàn)的碰撞檢測(cè)功能,涉及javascript鼠標(biāo)事件響應(yīng)及頁(yè)面圖形坐標(biāo)位置運(yùn)算、檢測(cè)相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Js md5加密網(wǎng)頁(yè)版MD5轉(zhuǎn)換代碼
Js實(shí)現(xiàn)網(wǎng)頁(yè)上的MD5加密功能,將文字轉(zhuǎn)換為MD5字符,本代碼調(diào)用簡(jiǎn)單,你可以新建一個(gè)網(wǎng)頁(yè),將此網(wǎng)頁(yè)上傳到你的服務(wù)器上,用戶(hù)瀏覽網(wǎng)頁(yè),就可實(shí)現(xiàn)MD5加密轉(zhuǎn)換功能,用戶(hù)可方便查詢(xún)?nèi)我蛔址腗D5碼,很不錯(cuò)的功能2013-03-03
javascript實(shí)現(xiàn)C語(yǔ)言經(jīng)典程序題
這篇文章主要介紹了javascript實(shí)現(xiàn)C語(yǔ)言經(jīng)典程序題的解題思路,感興趣的小伙伴們可以參考一下2015-11-11
JS實(shí)現(xiàn)不規(guī)則TAB選項(xiàng)卡效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)不規(guī)則TAB選項(xiàng)卡效果代碼,通過(guò)簡(jiǎn)單的JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)變換元素樣式實(shí)現(xiàn)不規(guī)則選項(xiàng)卡效果,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09
使用Bootstrap和Vue實(shí)現(xiàn)用戶(hù)信息的編輯刪除功能
這篇文章主要介紹了使用Bootstrap和Vue實(shí)現(xiàn)用戶(hù)信息的編輯刪除功能,需要的朋友可以參考下2017-10-10

