chrome extension popup cannot find element by ID

假如想象 提交于 2019-12-02 10:37:36

The problem is that your code executes as soon as <script> tag is read, i.e. before your element exists in DOM.

Wrap it in $(document).ready() and you're good to go:

$(document).ready(function() {
  /* your code */
});

For a non-jQuery solution, wrap it in DOMContentLoaded listener:

document.addEventListener("DOMContentLoaded", function() {
  /* your code */
});

Finally, you can simply move the <script> tag to the end of <body>, but it's a less robust solution.

I believe your popup.js syntax is wrong

  $('#btn').click(function (){
   alert("test"); 
};

should be

    $('#btn').click(function (){
   alert("test");
   });

looks like you are missing a paran;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!