Javascript error: Cannot read property 'parentNode' of null

前端 未结 2 1749
广开言路
广开言路 2021-02-19 07:27

The javascript I am using:

javascript: c = \'{unit}, ({coords}) {player} |{distance}| {return}\';
p = [\'Scout\', \'LC\', \'HC\', \'Axe\', \'Sword\', \'Ram\', \         


        
相关标签:
2条回答
  • 2021-02-19 08:04

    There are two possibilities:

    1. editInput is a typo, and the actual id of that element is different (ids are case-sensitive).
    2. You are executing this code while the DOM is not ready. To prevent this, execute the code just before the </body> closing tag, or wrap it in an event handler for the load event of window or the DOMContentLoaded event of document.

    EDITED How to wrap your code:

    window.onload = function() {
        //your code here
    };
    
    0 讨论(0)
  • 2021-02-19 08:07

    In my case it was a conflict between slider and form validator as they were located on different pages but javascript file would of target both of them simultaneously, so all pages that had no slider would come up with "Cannot read property 'parentNode' of null" error, so I have added to the slider If statement:

    if(document.getElementById("mainImage")){
    var myImage = document.getElementById("mainImage");
    var linkElement = myImage.parentNode;
    

    that solved my problem, try it

    0 讨论(0)
提交回复
热议问题