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

前端 未结 4 1812
既然无缘
既然无缘 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:25

    For all those facing a similar issue, I came across this same issue when i was trying to run a particular code snippet, shown below.

    
        
            
    
        
        
            

    https://codepen.io/pcwanderer/pen/MMEREr

    Looking at the error in the console for the above code.

    Since the document.getElementById is returning a null and as null does not have a attribute named appendChild, therefore a error is thrown. To solve the issue see the code below.

    
        
            
        
        
            

    https://codepen.io/pcwanderer/pen/pXWBQL

    I hope this helps. :)

提交回复
热议问题