js操作DOM--添加、刪除節(jié)點(diǎn)的簡(jiǎn)單實(shí)例
更新時(shí)間:2016年07月08日 11:07:29 投稿:jingxian
下面小編就為大家?guī)?lái)一篇js操作DOM--添加、刪除節(jié)點(diǎn)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
js removeChild() 用法
<body>
<p id="p1">welcome to <b>javascript</b> world !</p>
<script language="javascript" type="text/javascript">
<!--
function nodestatus(node)
{
var temp="";
if(node.nodeName!=null)
{
temp+="nodeName="+node.nodeName+"\n";
}
else temp+="nodeName=null \n";
if(node.nodeType!=null)
{
temp+="nodeType="+node.nodeType+"\n";
}
else temp+="nodeType=null \n";
if(node.nodeValue!=null)
{
temp+="nodeValue="+node.nodeValue+"\n";
}
else temp+="nodeValue=null \n";
return temp;
}
var parent=document.getElementById("p1");
var msg="父節(jié)點(diǎn) \n"+nodestatus(parent)+"\n";
//返回元素節(jié)點(diǎn)p的最后一個(gè)孩子
last=parent.lastChild;
msg+="刪除之前:lastChild--"+nodestatus(last)+"\n";
//刪除節(jié)點(diǎn)p的最后一個(gè)孩子,變?yōu)閎
parent.removeChild(last);
last=parent.lastChild;
msg+="刪除之后:lastChild--"+nodestatus(last)+"\n";
alert(msg);
-->
</script>
</body>
<html>
<head>
<title>js控制添加、刪除節(jié)點(diǎn)</title>
</head>
<script type="text/javascript">
var all;
function addParagraph() {
all = document.getElementById("paragraphs").childNodes;
var newElement = document.createElement("p");
var seq = all.length + 1;
//創(chuàng)建新屬性
var newAttr = document.createAttribute("id");
newAttr.nodeValue = "p" + seq;
newElement.setAttribute(newAttr);
//創(chuàng)建文本內(nèi)容
var txtNode = document.createTextNode("段落" + seq);
//添加節(jié)點(diǎn)
newElement.appendChild(txtNode);
document.getElementById("paragraphs").appendChild(newElement);
}
function delParagraph() {
all = document.getElementById("paragraphs").childNodes;
document.getElementById("paragraphs").removeChild(all[all.length -1]);
}
</script>
<style>
p{
background-color : #e6e6e6 ;
}
</style>
<body>
<center>
<input type="button" value="添加節(jié)點(diǎn)" onclick="addParagraph();"/>
<input type="button" value="刪除節(jié)點(diǎn)" onclick="delParagraph();"/>
<div id="paragraphs">
<p id="p1">段落1</p>
<p id="p2">段落2</p>
</div>
</center>
</body>
</html>
以上這篇js操作DOM--添加、刪除節(jié)點(diǎn)的簡(jiǎn)單實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解JS獲取HTML DOM元素的8種方法
- JavaScript獲取DOM元素的11種方法總結(jié)
- 通過(guò)JS動(dòng)態(tài)創(chuàng)建一個(gè)html DOM元素并顯示
- JS動(dòng)態(tài)創(chuàng)建DOM元素的方法
- 快速解決js動(dòng)態(tài)改變dom元素屬性后頁(yè)面及時(shí)渲染的問(wèn)題
- JavaScript DOM元素尺寸和位置
- JS選取DOM元素的簡(jiǎn)單方法
- 原生JavaScript來(lái)實(shí)現(xiàn)對(duì)dom元素class的操作方法(推薦)
- JS實(shí)現(xiàn)動(dòng)態(tài)添加DOM節(jié)點(diǎn)和事件的方法示例
- JavaScript DOM節(jié)點(diǎn)添加示例
- JavaScript DOM元素常見(jiàn)操作詳解【添加、刪除、修改等】
相關(guān)文章
使用JavaScript實(shí)現(xiàn)獲取audio時(shí)長(zhǎng)
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)獲取audio時(shí)長(zhǎng),并且轉(zhuǎn)換為分鐘00:00:00格式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
Swiper實(shí)現(xiàn)導(dǎo)航欄滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Swiper實(shí)現(xiàn)導(dǎo)航欄滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
input禁止鍵盤(pán)及中文輸入,但可以點(diǎn)擊
這篇文章主要介紹了<input>禁止鍵盤(pán)及中文輸入,但可以點(diǎn)擊。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
Javascript的嚴(yán)格模式strict mode詳細(xì)介紹
這篇文章主要介紹了Javascript的嚴(yán)格模式strict mode詳細(xì)介紹,重點(diǎn)介紹了嚴(yán)格模式的使用方法及使用strict mode后對(duì)javascript語(yǔ)法上帶來(lái)的改變,需要的朋友可以參考下2014-06-06
JavaScript實(shí)現(xiàn)的經(jīng)典文件樹(shù)菜單效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的經(jīng)典文件樹(shù)菜單效果,通過(guò)JavaScript結(jié)合json數(shù)組實(shí)現(xiàn)文件樹(shù)菜單的效果,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-09-09
js老生常談之this,constructor ,prototype全面解析
下面小編就為大家?guī)?lái)一篇js老生常談之this,constructor ,prototype。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-04-04

