Arn*_*rno 16 javascript jquery drupal
我尝试使用下面的代码,在我的网站上添加幻灯片中的按钮:
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");
}
Run Code Online (Sandbox Code Playgroud)
在首页上,我有幻灯片显示一切正常,但在其他页面上Cannot read property 'appendChild' of null发生错误.
eg_*_*dac 17
该元素尚未附加,因此它等于null.Id永远不会= 0.当你调用getElementById(id)时,它是null,因为它不是dom的一部分,除非你的静态id已经在DOM上了.通过控制台进行呼叫以查看它返回的内容.