DOM添加节点

 效果如图

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOMAdd</title>
</head>
<script type="text/javascript">
function addImg(node){
var newImg=document.createElement("img");//创建img 节点
newImg.setAttribute("src","/DOM/img/timg (1).jpg");//设置src属性
newImg.setAttribute("width","100");//设置宽属性
newImg.setAttribute("height","80");//设置高属性

if(node==null){
var docBody=document.getElementsByTagName("body");
docBody[0].appendChild(newImg);//在body末尾追加节点
}
else{
var docBody=document.getElementsByTagName("body");
docBody[0].insertBefore(newImg,node);//在node 前插入节点
}
}
</script>
<body>
<input type="button" value="在按钮前添加图片" onclick="addImg(this)"/>
<input type="button" value="在按钮后添加图片" onclick="addImg(null)"/><br />
</body>
</html>

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

原文地址:https://www.cnblogs.com/wxhhts/p/7895351.html