源代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="new_file.css"/>
<script src="new_file.js"></script>
<script >
window.onload=function(){
if(!document.getElementById) return false;
if(!document.getElementsByClassName) return false;
if(! document.getElementById("placeholder"));
var oImg = document.getElementById("placeholder");
var oShow = document.getElementsByClassName("show");
for(var i=0;i<oShow.length;i++){
oShow[i].onclick=function(){
var source = this.getAttribute("href");
if(oImg.getAttribute("src"))
oImg.setAttribute("src",source);
var oP = document.getElementById("description");
var desciption = this.getAttribute("title");
oP.childNodes[0].nodeValue = desciption;
return false;
}
}
var image = document.createElement("img");
image.setAttribute("id","placeholder");
image.setAttribute("src","image/u=1976971154,3672095769&fm=27&gp=0.jpg");
image.setAttribute("alt","my image gallery");
var description = document.createElement("p");
description.id = "description";
description.nodeValue = "趴着";
insertAfter(image,oImg);
insertAfter(description,image);
};
function insertAfter(newElement,targetElement) {
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement,targetElement.nextSibling);
}
}
</script>
<script type="text/javascript" src="ssss.js">
</script>
</head>
<body>
<h1>抱抱表情包</h1>
<div>
<ul id="imagegallery">
<li>
<a class="show" href="image/微信图片_20181211205206.jpg" title="趴着"><img src="image/微信图片_20181211205206.jpg" /></a>
</li>
<li>
<a class="show" href="image/微信图片_20181211205218.jpg" title="写字"><img src="image/微信图片_20181211205218.jpg" /></a>
</li>
<li>
<a class="show" href="image/微信图片_20181211205222.jpg" title="戳脸"><img src="image/微信图片_20181211205222.jpg"/></a>
</li>
<li>
<a class="show" href="image/微信图片_20181211205226.jpg" title="爱心"><img src="image/微信图片_20181211205226.jpg" /></a>
</li>
</ul></div>
</body>
</html>
你仔细看下这个
oImg
能取到dom?取不到dom的情况返回
null
,你把null
当参数传到insertAfter
里面再调用null.parentNode
不报错才怪