栗子:为body添加一个内容为sss的文本节点
document.body.appendChild(document.createTextNode("sss"))
2、移除节点,removeChild移除某个节点的子节点
栗子:
var ccn=document.getElementById("sd").childNodes[0]//获取到要移除的节点
document.getElementById("sd").removeChild(ccn)//将id为sd的第一个子节点移除
3、移动,控制
栗子:将id为sd的节点向右边移动50px
var sdds=document.getElementById("sd")
sdds.style.left=parseInt(sdds.style.left)+50+"px"
<div id='sd' style="position: absoluteleft: 10px">sd</div>
4、创建节点
document.createTextNode("sss")//创建文本节点
document.createElement("p")//创建p节点
5、查找节点
document.getElementById('oo')//根据id查找
document.getElementsByTagName("p")//根据标签名字查找节点
...
js *** 作节点的DOM方法
一、创建节点
createElement
createTextNode
二、添加、删除、替换、克隆元素
1、appendChild
2、insertBefore
3、如果appendChild、insertBefore追加的是已经存在的子元素,实际上就会将子元素位置移动
4、删除节点removeremoveChild
remove()
removeChild
5.替换节点
replaceChild
6、克隆节点
cloneNode()
三、 *** 作元素属性的方法
1.获取属性 元素.属性名
2.设置属性 元素.属性名 = 属性值
1、2方法只能获取和设置元素自带的属性
3. *** 作属性的万能方法
getAttribute()
setAttribute()
需要准备的材料分别是:电脑、html编辑器、浏览器。
1、首先,打开html编辑器,新建一个html文件,例如:index.html。
2、在index.html的<script>标签中,输入js代码:document.body.removeChild(document.getElementById("1"))。
3、浏览器运行index.html页面,此时会发现123所在的整个div以及它的内容都被移除了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)