js *** 作节点的DOM方法

js *** 作节点的DOM方法,第1张

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()

1、js添加节点:document.body.appendChild(document.createTextNode在写出想要添加的即可。

2、js移除节点:varccn=document.getElementById("sd").childNodes[0]//填写要移除的节点。

3、js移动节点:编写varsdds=document.getElementById("sd")即可移动。

4、js复制节点:document.getElementsByTagName("p")//即可复制。

5、js创建节点:document.createTextNode("sss")//即可进行创建。

document.createElement()是在对象中创建一个对象,要与appendChild() 或 insertBefore()方法联合使用。\x0d\x0a其中,appendChild() 方法在节点的子节点列表末添加新的子节点。insertBefore() 方法在节点的子节点列表任意位置插入新的节点。\x0d\x0a1、添加DIV \x0d\x0afunction addDiv(w,h){ \x0d\x0a//如果原来有“divCell”这个图层,先删除这个图层\x0d\x0adeleteDiv()\x0d\x0a//创建一个div \x0d\x0avar newdiv = document.createElement("divCell") \x0d\x0a//添加到页面 \x0d\x0adocument.body.appendChild(newdiv) \x0d\x0a//通过样式指定该div的位置方式,若是想要自己设置div的位置,这句话必须有,把它注释掉你就可以知道效果拉~试试看 \x0d\x0anewdiv.style.position="absolute" \x0d\x0a//通过样式指定x坐标(随机数0~450) \x0d\x0anewdiv.style.top= Math.round(Math.random()*450) \x0d\x0a//通过样式指定y坐标(随机数0~700) \x0d\x0anewdiv.style.left= Math.round(Math.random()*700) \x0d\x0a//通过样式指定宽度 \x0d\x0anewdiv.style.width=w \x0d\x0a//通过样式指定高度 \x0d\x0anewdiv.style.height=h \x0d\x0a//通过样式指定背景颜色,,若是背景图片 例为 newdiv.style.backgroundImage="url(img/3.jpg)" \x0d\x0anewdiv.style.backgroundColor="#ffffcc" \x0d\x0a//添加div的内容 \x0d\x0a//newdiv.innerHTML=i++\x0d\x0a//设置样式透明\x0d\x0anewdiv.style.filter = "alpha(opacity=50)"\x0d\x0a//设置ID\x0d\x0anewdiv.id = "divCell" \x0d\x0a }\x0d\x0a2、删除DIV \x0d\x0a function deleteDiv()\x0d\x0a {\x0d\x0avar my = document.getElementById("divCell")\x0d\x0aif (my != null)\x0d\x0amy.parentNode.removeChild(my)\x0d\x0a }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11791169.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存