[英]onclick not working for Chrome Extension?
I'm trying to create an extension for Google Chrome, and onclick method does not seem to be working. 我正在尝试为Google Chrome创建扩展程序,并且onclick方法似乎不起作用。 I have an html file popup.html:
我有一个html文件popup.html:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Before clicking button</p>
<button type="button" id="thing">Pls work</button>
<script src="popup.js"></script>
</body>
</html>
And JavaScript file popup.js: 和JavaScript文件popup.js:
var a =0;
function myFunction()
{
a= a +1;
document.getElementById('demo').textContent=a;
}
document.getElementById('thing').onclick = myFunction();
And, finally, a manifest file: 最后,一个清单文件:
{
"name": "Hello World!",
"version": "1.0",
"manifest_version": 2,
"description": "My first Chrome extension.",
"browser_action": {
"default_icon": "shark.png",
"default_popup": "popup.html"
}
}
When I load this extension onto Chrome and then click on the extension, it presents an extension window that goes straight to saying "1", rather than the message "Before clicking button." 当我将此扩展程序加载到Chrome上然后单击该扩展程序时,它会显示一个直接显示“1”的扩展程序窗口,而不是“单击按钮之前”消息。 Then, when I try to click the button, nothing happens- that is, the text remains 1, and does not increment like it should.
然后,当我尝试单击按钮时,没有任何反应 - 也就是说,文本保持为1,并且不会像应该的那样增加。
Thank you so much!! 非常感谢!!
Try this : 尝试这个 :
JS: JS:
window.onload = function() {
var a =0;
function myFunction()
{
a= a +1;
document.getElementById('demo').textContent=a;
}
document.getElementById('thing').onclick = myFunction;
}
HTML HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="popup.js"></script>
</head>
<body>
<p id="demo">Before clicking button</p>
<button type="button" id="thing">Pls work</button>
</body>
</html>
The only think that needs to change is 唯一需要改变的想法是
document.getElementById('thing').onclick = myFunction();
to 至
document.getElementById('thing').onclick = myFunction;
The first on executes the function instead of setting the function to execute for the onclick event. 第一个on执行函数而不是设置要为onclick事件执行的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.