我创建了一个有趣的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>
这是js文件中的代码
function FncZoomIn()
{
window.open("https://www.google.com.vn/");
chrome.tabs.getZoom(1);
}
function FncZoomOut()
{
chrome.tabs.getZoom(-10);
}
答案 0 :(得分:0)
Chrome插件(firefox插件,可能还有所有其他插件)因安全问题而不允许使用内联javascript。
如果要在不使用inline-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);