How do I solve this “Cannot read property 'appendChild' of null” error?

前端 未结 4 1807
既然无缘
既然无缘 2021-02-05 09:46

I tried to use this code below, which adds buttons in slideshow on my site:

window.onload = function loadContIcons() {
    var elem = document.createElement(\"im         


        
4条回答
  •  醉酒成梦
    2021-02-05 10:21

    The element hasn't been appended yet, therefore it is equal to null. The Id will never = 0. When you call getElementById(id), it is null since it is not a part of the dom yet unless your static id is already on the DOM. Do a call through the console to see what it returns.

提交回复
热议问题