chrome extension popup cannot find element by ID

后端 未结 2 1063
悲&欢浪女
悲&欢浪女 2021-01-24 15:17

I know similar questions have been asked many times, but I didn\'t find a solution for mine yet. My question is really simple. All I want to do is to test actions on popup.html,

相关标签:
2条回答
  • 2021-01-24 15:30

    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.

    0 讨论(0)
  • 2021-01-24 15:38

    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;

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