简体   繁体   中英

cannot call method appendChild of undefined

I get this error when I insert aa href tag inside innerHtml in the javascript (flickrshow-7.1.js), for inserting a link for an image. Images are pulled from the flickr account using flickrshow javascript and displayed in the website as slideshow.

a.onLoadWindow = function() {
    a.elements.target = typeof a.elements.target === "string" ? document.getElementById(a.elements.target) : a.elements.target;
    a.elements.target.innerHTML = '<a href="http://www.google.com"><div class="flickrshow-container" style="background:transparent;height:' + a.elements.target.offsetHeight + "px;margin:0;overflow:hidden;padding:0;position:relative;width:" + a.elements.target.offsetWidth + 'px"></div></a>';

The undefined error means the javascript variable you are tying to call appendChild on doesn't have a value. You need to fix the initialization so it has a value on which to call appendChild

<p id="parent"></p>

...

var newNode = document.createElement("li");
var parent; 
parent.appendChild(newNode);  // Error!!! What is parent?
parent = document.getElementById("parent");
parent.appendChild(newNode);  

An img element has no innerHTML, and you can not append a child node to an img. Wrap the images in the a elements instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM