popup.html中的按钮不起作用 [英] button in popup.html not working
问题描述
我有一个popup.html,并且我认为这是非常简单的监听事件。
这是我的popup.html:
<!DOCTYPE html>
< html>
< body>
< button id =button>立即开始< / button>
< script>
函数blah(){
alert(blah);
$ b $(document).ready(function(){
document.getElementById('button')。addEventListener(click,blah);
});
< / script>
< / body>
< / html>
按下按钮时没有提示;我做错了什么?
您的代码可以从正常的网页观点来看,但从Chrome扩展指南您需要检查内容安全政策(CSP),其中说:
内联JavaScript不会被执行。此限制禁止
inline< script>
块和内联事件处理程序(例如< button onclick =...> ;
)。
所以,你可以做什么来测试你的代码是将脚本代码放在分开 .js
文件,然后在你的html中引用它。
正如 Xan 中有关使用 alert()
:
取代 alert()
,您可以使用简单的控制台。 log()
,如果目的只是测试按钮点击或者如果你真的想弹出警告像东西,然后创建一个模式对话框。
仅供参考(可能OP已经意识到它):
由于您使用 jQuery
检查这个指南也关于加载jQuery或其他库。
I have a popup.html, and what I thought was pretty simple listener event.
This is my 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:
Inline JavaScript will not be executed. This restriction bans both inline
<script>
blocks and inline event handlers (e.g.<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.
As suggested by Xan in comments about the usage of 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.
Just for reference only (Probably OP is already aware of it though):
Since you are using jQuery
check this guideline also about loading jQuery or other libraries.
这篇关于popup.html中的按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!