JavaScript DOM节点操作实例小结(新建,删除HTML元素)
作者:books1958 时间:2024-04-17 10:37:57
本文实例讲述了JavaScript DOM节点操作方法。分享给大家供大家参考,具体如下:
使用DOM可以新建HTML元素,也可以删除已有的HTML元素。
(一)新建元素:
<script>
//创建新的 <p> 元素
var newEle=document.createElement("p");
//创建文本节点
var node=document.createTextNode("这是使用Javascript创建的新段落。");
//将文本节点添加到新创建的 <p> 元素中
newEle.appendChild(node);
var div1_ele=document.getElementById("div1");
//将新创建的元素添加到已有的元素中
div1_ele.appendChild(newEle);
</script>
(二)删除HTML元素
<script>
//删除一个元素时,必须首先获取到它的父元素
function deleteEle(){
var parent = document.getElementById("div_02");
var child = document.getElementById("div_02_p2");
parent.removeChild(child);
}
//使用代码获取元素的父元素
function deleteEle3(){
var child = document.getElementById("div_02_p3");
child.parentNode.removeChild(child);
}
</script>
效果图:
示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>Javascript 节点</title>
<head>
<style>
body {background-color:#e6e6e6}
</style>
</head>
<body>
<h3>(一)添加元素</h3>
<div id="div1">
<p id="p1">这是一个段落</p>
<p id="p2">这是另一个段落</p>
</div>
<script>
//创建新的 <p> 元素
var newEle=document.createElement("p");
//创建文本节点
var node=document.createTextNode("这是使用Javascript创建的新段落。");
//将文本节点添加到新创建的 <p> 元素中
newEle.appendChild(node);
var div1_ele=document.getElementById("div1");
//将新创建的元素添加到已有的元素中
div1_ele.appendChild(newEle);
</script>
<h3>(二)删除元素</h3>
<div id="div_02">
<p id="div_02_p1">这是一个段落</p>
<p id="div_02_p2">这是另一个段落</p>
<button onclick="deleteEle()">删除id为‘div_02_p2'的元素</button><br/>
<p id="div_02_p3">这是第三个段落</p>
<button onclick="deleteEle3()">删除id为‘div_02_p3'的元素(自动获取父元素)</button>
<script>
//删除一个元素时,必须首先获取到它的父元素
function deleteEle(){
var parent = document.getElementById("div_02");
var child = document.getElementById("div_02_p2");
parent.removeChild(child);
}
//使用代码获取元素的父元素
function deleteEle3(){
var child = document.getElementById("div_02_p3");
child.parentNode.removeChild(child);
}
</script>
</div>
</body>
</html>
希望本文所述对大家JavaScript程序设计有所帮助。
标签:JavaScript,DOM节点
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaScript 映射器 array.flatMap()
2024-05-22 10:40:10
Mybatis多表查询与动态SQL特性详解
2024-01-22 20:54:44
![](https://img.aspxhome.com/file/2023/5/77975_0s.jpg)
Pthon批量处理将pdb文件生成dssp文件
2021-10-07 13:11:04
Python文件的应用之序列化与反序列化详解
2021-06-19 22:20:45
![](https://img.aspxhome.com/file/2023/1/92251_0s.jpg)
python 按照sheet合并多个Excel的示例代码(多个sheet)
2022-07-13 05:20:20
![](https://img.aspxhome.com/file/2023/9/130619_0s.png)
js页面文字选中后分享到新浪微博实现
2011-03-17 12:55:00
利用python实现简单的情感分析实例教程
2021-12-27 18:19:42
![](https://img.aspxhome.com/file/2023/0/135320_0s.png)
Python3简单实例计算同花的概率代码
2023-07-20 16:23:03
![](https://img.aspxhome.com/file/2023/6/70746_0s.png)
Python字典和列表性能之间的比较
2022-08-08 12:49:58
![](https://img.aspxhome.com/file/2023/0/128630_0s.png)
go结构体嵌套的切片数组操作
2024-02-09 12:38:57
CentOS7系统搭建LAMP及更新PHP版本操作详解
2024-05-22 10:07:38
![](https://img.aspxhome.com/file/2023/4/124594_0s.png)
Pycharm 字体大小调整设置的方法实现
2023-10-23 19:35:11
![](https://img.aspxhome.com/file/2023/4/135004_0s.png)
Python2.7简单连接与操作MySQL的方法
2024-01-25 18:38:56
python字典key不能是可以是啥类型
2022-12-28 20:54:04
Python 中的 Counter 模块及使用详解(搞定重复计数)
2022-08-22 15:46:17
vue使用watch 观察路由变化,重新获取内容
2024-05-05 09:11:16
![](https://img.aspxhome.com/file/2023/2/130112_0s.gif)
PHP实现克鲁斯卡尔算法实例解析
2023-09-08 19:35:57
Python数据结构之列表与元组详解
2022-05-30 00:39:04
![](https://img.aspxhome.com/file/2023/1/123511_0s.png)
Python实现基于C/S架构的聊天室功能详解
2022-06-14 11:43:38
![](https://img.aspxhome.com/file/2023/5/78635_0s.png)
JavaScript Try...Catch 声明的 使用方法
2024-04-18 10:52:21