这次给大家带来JS添加元素新节点,JS添加元素新节点的注意事项有哪些,下面就是实战案例,一起来看一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!DOCTYPE html>
<html lang= "en" >
<head>
<meta charset= "UTF-8" >
<title>www.jb51.net - JS添加新节点的几种方法</title>
</head>
<body>
<p id= "d" >
<span id= "s" >
1234567890
</span>
</p>
</body>
<script type= "text/javascript" >
var d=document.getElementById( 'd' );
var a=document.createElement( 'a' );
a.href= 'https://www.baidu.com/' ;
a.innerText= 'ggggg' ;
d.appendChild(a);
var p=document.createElement( 'p' );
p.innerText= 'ppppppppppppppppp' ;
d.insertBefore(p,a);
var s=document.getElementById( 's' );
var spanc= s.cloneNode(true);
d.appendChild(spanc);
</script>
</html>
|
登录后复制
运行效果截图:

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
JS运动缓冲效果的封装函数如何使用
JavaScript的异步加载详解
以上是JS添加元素新节点的详细内容。更多信息请关注PHP中文网其他相关文章!