【发布时间】:2014-07-30 13:33:10
【问题描述】:
我尝试使用下面的代码,它在我的网站上的幻灯片中添加按钮:
window.onload = function loadContIcons() {
var elem = document.createElement("img");
elem.src = "http://arno.agnian.com/sites/all/themes/agnian/images/up.png";
elem.setAttribute("class", "up_icon");
var id = "views_slideshow_controls_text_next_slideshow-block";
if (id !== 0) {
document.getElementById(id).appendChild(elem);
} else console.log("aaaaa");
var elem1 = document.createElement("img");
elem1.src = "http://arno.agnian.com/sites/all/themes/agnian/images/down.png";
elem1.setAttribute("class", "down_icon");
var id1 = "views_slideshow_controls_text_previous_slideshow-block";
if (id1 !== 0) {
document.getElementById(id1).appendChild(elem1);
} else console.log("aaaaa");
}
在首页,我有幻灯片,一切正常,但在其他页面上出现错误Cannot read property 'appendChild' of null。
【问题讨论】:
-
你能发布你的网站代码吗?
-
我不明白该错误消息如何没有清楚地传达问题。每当其中一个
getElementById()调用返回null时,您都会收到该错误。 -
使用 chrom 开发工具和断点进行调试。
-
此外,在将“id”和“id1”设置为其中一个长字符串后立即不等于零的那些测试没有意义。
-
与其检查
id !== 0是否永远不会为假,不如检查document.getElementById()是否返回了什么?
标签: javascript jquery drupal