我尝试在下面使用此代码,该代码在我的网站上的幻灯片中添加了按钮:
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
会发生错误。
该元素尚未添加,因此等于null。Id永远不会=0。调用getElementById(id)时,它为null,因为它尚不是dom的一部分,除非您的静态ID已在DOM上。通过控制台进行呼叫以查看返回的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句