首页 > 设计 > WEB开发 > 正文

js添加、移除、移动、复制、创建节点

2019-11-02 18:23:34
字体:
来源:转载
供稿:网友
1、添加节点append方法例子:为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的节点向右边移动50pxvar sdds=document.getElementById("sd");
sdd.style.background='blue';    sdds.style.left=parseInt(sdds.style.left)+50+"px";<div id='sd' style="position: absolute; left: 10px;">sd</div>
4、创建节点document.createTextNode("sss")//创建文本节点document.createElement("p")//创建p节点
p.innerText = 'ppppp';
sdds.appendChild(p);
5、查找节点document.getElementById('oo');//根据id查找
document.getElementByClassName('dd')[0];document.getElementsByTagName("p")[0];//根据标签名字查找节点


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表