document.querySelector always returns null

前端 未结 4 1757
野的像风
野的像风 2021-01-20 01:38

I\'ve looked at the other answers but still don\'t understand.

For some reason this line of code always returns null.

var els = document.querySelecto         


        
4条回答
  •  耶瑟儿~
    2021-01-20 02:12

    Possible solutions to your problem...

    IIFE

    (function () {
      var els = document.querySelector("[id='MTG_INSTR$2']");
      console.log(els);
    })();
    

    DOMContentLoaded

    document.addEventListener("DOMContentLoaded", function () {
      var els = document.querySelector("[id='MTG_INSTR$2']");
      console.log(els);
    });
    

提交回复
热议问题