使用insertBefore函数后报错Cannot read property 'parentNode' of null"

clipboard.png

源代码

<!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>
阅读 7.1k
1 个回答
 var oImg = document.getElementById("placeholder");

你仔细看下这个oImg能取到dom?
取不到dom的情况返回null,你把null当参数传到insertAfter里面再调用null.parentNode不报错才怪

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏