<i id='AbXZW'><tr id='AbXZW'><dt id='AbXZW'><q id='AbXZW'><span id='AbXZW'><b id='AbXZW'><form id='AbXZW'><ins id='AbXZW'></ins><ul id='AbXZW'></ul><sub id='AbXZW'></sub></form><legend id='AbXZW'></legend><bdo id='AbXZW'><pre id='AbXZW'><center id='AbXZW'></center></pre></bdo></b><th id='AbXZW'></th></span></q></dt></tr></i><div id='AbXZW'><tfoot id='AbXZW'></tfoot><dl id='AbXZW'><fieldset id='AbXZW'></fieldset></dl></div>
    • <bdo id='AbXZW'></bdo><ul id='AbXZW'></ul>

    <tfoot id='AbXZW'></tfoot>
    1. <small id='AbXZW'></small><noframes id='AbXZW'>

      <legend id='AbXZW'><style id='AbXZW'><dir id='AbXZW'><q id='AbXZW'></q></dir></style></legend>

        chrome 扩展弹出窗口无法按 ID 找到元素

        chrome extension popup cannot find element by ID(chrome 扩展弹出窗口无法按 ID 找到元素)

        <small id='kFVjL'></small><noframes id='kFVjL'>

          <tbody id='kFVjL'></tbody>
          <i id='kFVjL'><tr id='kFVjL'><dt id='kFVjL'><q id='kFVjL'><span id='kFVjL'><b id='kFVjL'><form id='kFVjL'><ins id='kFVjL'></ins><ul id='kFVjL'></ul><sub id='kFVjL'></sub></form><legend id='kFVjL'></legend><bdo id='kFVjL'><pre id='kFVjL'><center id='kFVjL'></center></pre></bdo></b><th id='kFVjL'></th></span></q></dt></tr></i><div id='kFVjL'><tfoot id='kFVjL'></tfoot><dl id='kFVjL'><fieldset id='kFVjL'></fieldset></dl></div>

                <bdo id='kFVjL'></bdo><ul id='kFVjL'></ul>
                <tfoot id='kFVjL'></tfoot>

                  <legend id='kFVjL'><style id='kFVjL'><dir id='kFVjL'><q id='kFVjL'></q></dir></style></legend>
                  本文介绍了chrome 扩展弹出窗口无法按 ID 找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道类似的问题已经被问过很多次,但我还没有找到适合我的解决方案.我的问题很简单.我要做的就是测试 popup.html 上的操作,因为在这里,我在弹出窗口上有一个单击按钮,当我单击它时,我想显示警报.但是什么也没发生.它没有找到元素.我不明白这里出了什么问题.

                  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, for here, I have a click button on popup, when I click it, I want to show alert. But nothing happened. It's not finding the element. I don't understand what's going wrong here.

                  manefest.json

                  {
                    "name": "test",
                    "version": "1.0",
                    "description": "test",
                    "manifest_version":2,
                    "browser_action": {
                      "default_icon": "logo.png",
                      "default_popup":"popup.html"
                    },
                    "permissions": [
                      "tabs", 
                      "http://*/*",
                      "notifications"
                    ]
                  }
                  

                  popup.html

                  <html>
                    <head>
                      <title>Test</title>
                      <script type="text/javascript" src="jquery.js"></script> 
                      <script type="text/javascript" src="popup.js"></script>
                    </head>
                    <body>
                      <button id='btn'>click</button>
                    </body>
                  </html>
                  

                  popup.js

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

                  推荐答案

                  问题是你的代码在 <script> 标签被读取后立即执行,即在你的元素存在于 DOM 之前.

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

                  将它包装在 $(document).ready() 中就可以了:

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

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

                  对于非 jQuery 解决方案,将其包装在 DOMContentLoaded 监听器中:

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

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

                  最后,您可以简单地将 <script> 标记移动到 <body> 的末尾,但这是一个不太可靠的解决方案.

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

                  这篇关于chrome 扩展弹出窗口无法按 ID 找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                    <small id='oRWgQ'></small><noframes id='oRWgQ'>

                    <i id='oRWgQ'><tr id='oRWgQ'><dt id='oRWgQ'><q id='oRWgQ'><span id='oRWgQ'><b id='oRWgQ'><form id='oRWgQ'><ins id='oRWgQ'></ins><ul id='oRWgQ'></ul><sub id='oRWgQ'></sub></form><legend id='oRWgQ'></legend><bdo id='oRWgQ'><pre id='oRWgQ'><center id='oRWgQ'></center></pre></bdo></b><th id='oRWgQ'></th></span></q></dt></tr></i><div id='oRWgQ'><tfoot id='oRWgQ'></tfoot><dl id='oRWgQ'><fieldset id='oRWgQ'></fieldset></dl></div>
                  • <legend id='oRWgQ'><style id='oRWgQ'><dir id='oRWgQ'><q id='oRWgQ'></q></dir></style></legend>

                        <tbody id='oRWgQ'></tbody>

                        • <bdo id='oRWgQ'></bdo><ul id='oRWgQ'></ul>
                          <tfoot id='oRWgQ'></tfoot>