Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript chrome扩展弹出页面的我的按钮没有收到事件,错误是什么?_Javascript_Html_Google Chrome - Fatal编程技术网

Javascript chrome扩展弹出页面的我的按钮没有收到事件,错误是什么?

Javascript chrome扩展弹出页面的我的按钮没有收到事件,错误是什么?,javascript,html,google-chrome,Javascript,Html,Google Chrome,我创建chrome扩展是为了好玩,但我有个问题。如果我用Chrome打开popup.html,这个页面的按钮会起作用,但是当我用Chrome的扩展打开它时,这个按钮就不起作用了。谁能帮我解释一下这里有什么问题,谢谢 下面是popup.html中的代码 <html> <head> <title>Blank New Tab</title> <style> div { color: #cccccc; vertical-align: 50%

我创建chrome扩展是为了好玩,但我有个问题。如果我用Chrome打开popup.html,这个页面的按钮会起作用,但是当我用Chrome的扩展打开它时,这个按钮就不起作用了。谁能帮我解释一下这里有什么问题,谢谢

下面是popup.html中的代码

<html>
<head>
<title>Blank New Tab</title>
<style>
div {
 color: #cccccc;
 vertical-align: 50%;
 text-align: center;
 font-family: sans-serif;
 font-size: 300%;
     }
 </style>
 <script src="my_popup.js"></script>
 </head>
  <body>
    <div style="height:40%"></div>
    <table>
      <tr>
       <td><button onclick="FncZoomIn();" id="btnZoomIn">ZoomIn</button>  </td>
       <td><button onclick="click" id="btnReset">Reset</button>  </td>
       <td><button onclick="click" id="btnZoomOut">ZoomOut</button>  </td>
      </tr>
    </table>
  </body>

Chrome插件firefox插件,可能所有其他插件也不允许内联javascript,因为存在安全问题

如果要在不使用内联javascript的情况下将事件处理程序附加到按钮,请执行以下操作:

// looks for an element using its ID
var zoomButton = document.getElementById("btnZoomIn");

// the button is now listening for click events
zoomButton.addEventListener("click", function (event) {  
  // this function will be called whenever the button is clicked.
  // if you have already written the function, you can also do the following
});

// FncZoomIn will be called whenever the button is clicked
zoomButton.addEventListener("click", FncZoomIn);

插件不允许内联javascript。js文件之外的javascript将无法工作。请尝试按按钮的ID选择按钮,并向其添加onclick事件侦听器。您能给我举个例子吗?非常感谢!如果我使用扩展就不行了!在网页中,它可以工作,但在chrome扩展的弹出页面中,它不能工作,我不知道为什么?
// looks for an element using its ID
var zoomButton = document.getElementById("btnZoomIn");

// the button is now listening for click events
zoomButton.addEventListener("click", function (event) {  
  // this function will be called whenever the button is clicked.
  // if you have already written the function, you can also do the following
});

// FncZoomIn will be called whenever the button is clicked
zoomButton.addEventListener("click", FncZoomIn);