简体   繁体   English

popup.html中的按钮不起作用

[英]button in popup.html not working

I have a popup.html, and what I thought was pretty simple listener event. 我有一个popup.html,我认为这是非常简单的监听器事件。

This is my popup.html: 这是我的popup.html:

<!DOCTYPE html>
<html>
<body>
  <button id="button">Starting Now</button>

  <script>
    function blah(){
      alert("blah");
    }

    $(document).ready(function(){ 
      document.getElementById('button').addEventListener("click", blah);
    });
  </script>
</body>
</html>

There's no alert when I press the button; 当我按下按钮时没有警报; what am I doing wrong? 我究竟做错了什么?

Your code is okay from normal web-page point of view but from the guidelines of Chrome Extension Development you need to check Content Security Policy (CSP) which says: 从正常的网页角度来看,您的代码是可以的,但是根据Chrome扩展程序开发的准则,您需要检查内容安全政策(CSP) ,其中指出:

Inline JavaScript will not be executed. 内联JavaScript将不会执行。 This restriction bans both inline <script> blocks and inline event handlers (eg <button onclick="..."> ). 此限制禁止内联<script>块和内联事件处理程序(例如<button onclick="..."> )。

So, what you can do to test your code is place the script code in a separate .js file and then refer it in your html. 因此,您可以将脚本代码放在单独的.js文件中,然后在html中进行引用,以测试代码。

As suggested by Xan in comments about the usage of alert() : 正如Xan在有关alert()用法的评论中所建议的那样:
In place of alert() you can use simple console.log() if the purpose is only to test the button click or else if you really want pop-up alert like thing then create a modal dialog. 如果目的只是为了测试按钮单击,或者如果您确实想要像这样的弹出警报,然后创建一个模式对话框,则可以使用简单的console.log()代替alert()


Just for reference only ( Probably OP is already aware of it though ): 仅供参考虽然OP可能已经知道了 ):

Since you are using jQuery check this guideline also about loading jQuery or other libraries . 由于您使用的是jQuery 请同时检查有关加载jQuery或其他库的本指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM