当前位置:网站首页>DOM — 元素的增删改查
DOM — 元素的增删改查
2022-08-02 14:17:00 【z_小张同学】
1.创建元素:document.createElement()用 此方法创建的元素只保存在内存中,而不会渲染到页面上。
// 创建一个div标签
var box2 = document.createElement("div")
// createElement()括号内填入的是要创建的标签名2.给标签添加类名:
有两种方法:(1)直接使用 x.className给标签添加
// 创建一个div标签
// createElement()括号内填入的是要创建的标签名
var box2 = document.createElement("div")
// 添加类名
box2.className = "box2"(2)使用 x.classList.add()方法,此方法的优点是可以多次调用,给元素添加多个类名,并且当多次调用添加的类名相同时,只会添加相同的一个类名
// 创建一个div标签
// createElement()括号内填入的是要创建的标签名
var box2 = document.createElement("div")
// 添加类名,给div设置了两个类名
box2.classList.add("box2")
box2.classList.add("box3")并且要删除类名时,也可以调用 x.classList.remove()来删除某个类名
// 创建一个div标签
// createElement()括号内填入的是要创建的标签名
var box2 = document.createElement("div")
// 添加类名,给div设置了两个类名
box2.classList.add("box2")
box2.classList.add("box3")
// 删除一个类名
box2.classList.remove("box3")3. 将创建的元素添加到文档树中,也就是渲染到页面上。
使用 x.appendChild(y) 把y节点对象添加到x节点中
// 获取
var box = document.querySelector("#box")
// 创建一个div标签
// createElement()括号内填入的是要创建的标签名
var box2 = document.createElement("div")
// 添加类名
// box2.className = "box2"
box2.classList.add("box2")
box2.classList.add("box3")
// 删除一个类名
box2.classList.remove("box3")
// 添加到文档树
box.appendChild(box2)4.删除元素
(1)父元素删除子元素
var box = document.querySelector("#box")
// 1.父元素删除子元素
box.parentElement.removeChild(box)(2)元素自己移除
var box = document.querySelector("#box")
// 2.自己移除
box.remove()(3)元素直接全部清空自己
var box = document.querySelector("#box")
// 3.清空自己
box.parentElement.innerHTML = ""5.克隆: 使用 x.cloneNode() 方法
var box = document.querySelector("#box")
// var box2 = box.cloneNode(true) //参数为true,会连同box的后代元素和所有事件 一起克隆
var box2 = box.cloneNode() //不会克隆事件等
box.appendChild(box2)6.插入节点: x.insertBefore()
<body>
<div class="box">
<div class="box2">aaa</div>
<div class="box2">bbb</div>
<div class="box2">ccc</div>
<div class="box2">ddd</div>
</div>
<script>
var box = document.querySelector(".box")
var a1 = document.createElement("h1")
a1.innerHTML = 666
// 在box的下标为2的子元素之前添加一个元素
box.insertBefore(a1,box.children[2])
</script>
</body>
边栏推荐
猜你喜欢

RTMP, RTSP, SRT 推流和拉流那些事

Apache ShardingSphere 5.1.1 正式发布

How to tick the word box?
![The relationship between base classes and derived classes [inheritance] / polymorphism and virtual functions / [inheritance and polymorphism] abstract classes and simple factories](/img/c1/c695006706ce91233d9ac8ecb95c50.png)
The relationship between base classes and derived classes [inheritance] / polymorphism and virtual functions / [inheritance and polymorphism] abstract classes and simple factories

VLAN原理

软件测试之WEB自动化

flask获取post请求参数

这几年让你大呼惊人的AI应用,都离不开这项技术

webrtc 有关 SDP 部分的解析流程分析

hybrid 实现同网段但不同vlan之间通讯
随机推荐
JMM&synchronized&volatile详解
Object.defineProperty方法(详解)
filebeat的配置
webrtc 中怎么根据 SDP 创建或关联底层的 socket 对象?
打包项目上传到PyPI
【网络安全】学习笔记 --00
JVM常量池详解
面试追问系列-Redis技术原理
smart rtmpd web 接口说明
小知识点系列:数组与多维数组
CDH (computational Diffie-Hellman) problem and its differences with discrete logarithm and DDH problems
HCIE学习记录——数据封装与常用协议(TCP/UDP)
小知识点系列-基于H2数据库单元测试
一线大厂研发流程(转载自鱼皮)
makefile——library
解决启动filebeat时遇到Exiting: error unpacking config data: more than one namespace configured accessing错误
一个简单的 erlang 的 udp 服务器和客户端
WEB自动化之键盘、鼠标操作
smart_rtmpd 的 VOD 接口使用说明
Mysql索引底层数据结构