[英]Javascript Button onClick not working in Chrome extension popup
(link to git: https://github.com/Boundarybreaker/NameBlock ) (链接到git: https : //github.com/Boundarybreaker/NameBlock )
I'm working on a text-replacement extension that can be toggled on and off with a button clicked in the popup.html
, but it only is happening when I click the icon. 我正在开发一个文本替换扩展程序,可以通过在
popup.html
单击一个按钮来打开和关闭它,但这仅在单击图标时发生。 (video: https://drive.google.com/file/d/0B_3pLT_KI8V8OHJFUHQ5a1JURkE/view?usp=sharing ) (视频: https : //drive.google.com/file/d/0B_3pLT_KI8V8OHJFUHQ5a1JURkE/view?usp=sharing )
The code for the button reads <button class="ui fluid red button" onclick=toggleActive() id="toggle">Toggle Active</button>
, and toggleActive()
is a function in background.js
, along with a listener chrome.runtime.onMessage.addListener(toggleActive);
该按钮的代码读取
<button class="ui fluid red button" onclick=toggleActive() id="toggle">Toggle Active</button>
, toggleActive()
是background.js
一个函数,以及一个侦听器chrome.runtime.onMessage.addListener(toggleActive);
. 。 How would I get the button to work properly?
我将如何使按钮正常工作?
onclick is a kind of inline scripting, so it's forbidden in popup. onclick是一种内联脚本,因此在弹出窗口中被禁止。
Use myButton.addEventListener('click',toggleActive);
使用
myButton.addEventListener('click',toggleActive);
in your popup.js Obtain myButton
in any usual way, by adding an id to its html and using document.getElementById
, or by getElementsByClassName, querySelectorAll
or any other ways. 在您的popup.js中,可以通过任何常规方式获取
myButton
,方法是在其html中添加一个id并使用document.getElementById
,或者通过getElementsByClassName, querySelectorAll
或任何其他方式。
toggleActive
defined in popup.js can either send message to background.js, where the listener would call background's toggleActive
, or you can just get background page with chrome.runtime.getBackgroundPage
or chrome.extension.getBackgroundPage
(the former is async, the latter is sync, returning window object of background) and call your background toggleActive
from here, like this: toggleActive
定义的toggleActive可以将消息发送到background.js,侦听器将在其中调用背景的toggleActive
,或者您可以使用chrome.runtime.getBackgroundPage
或chrome.extension.getBackgroundPage
获取背景页面(前者是异步的,后者是同步的,返回背景的窗口对象), toggleActive
从此处调用背景toggleActive
,如下所示:
var bkg=chrome.extenstion.getBackgroundPage(); bkg.toggleActive();
That is, if you really need defining your toggleActive in background instead of popup. 也就是说,如果您确实需要在后台而不是弹出窗口中定义toggleActive。
I think you should do that: 我认为您应该这样做:
<button class="ui fluid red button" onclick="toggleActive()" id="toggle">Toggle Active</button>
You forget the parentheses after onclick. 单击onclick后,您会忘记括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.