简体   繁体   English

无法调用未定义的方法appendChild

[英]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. 当我在javascript(flickrshow-7.1.js)的innerHtml内插入aa href标记以插入图像链接时,会出现此错误。 Images are pulled from the flickr account using flickrshow javascript and displayed in the website as slideshow. 使用flickrshow javascript从flickr帐户中提取图像,并以幻灯片形式显示在网站上。

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. undefined错误表示您要在其上调用appendChild的javascript变量没有值。 You need to fix the initialization so it has a value on which to call appendChild 您需要修复初始化,以便它具有可调用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. img元素没有innerHTML,并且不能将子节点附加到img。 Wrap the images in the a elements instead. 而是将图像换成a元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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