javascript Chrome 扩展程序:onclick() 事件未触发 alert() 弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15766495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:02:33  来源:igfitidea点击:

Chrome extension: onclick() event not triggering alert() popup

javascriptjavascript-eventsgoogle-chrome-extensionevent-handlinggoogle-chrome-app

提问by anandh199g

Unable to trigger alert() popup with onclick() event

无法使用 onclick() 事件触发 alert() 弹出窗口

code:

代码:

manifest.json:

清单.json:

{
"name": "Project",
"version": "1.0.0",
"manifest_version": 2,
"description": "Popup when website requires Log in",
"browser_action":{
    "default_icon":"icon_19.png",
    "default_popup":"Popup.html"
    }
}

Popup.html:

弹出窗口.html:

<html>
<head></head>
<body>
<div class="plus" onclick="popup()"></div>
<script src="inline.js"></script>
</body>
</html>

inline.js:

内联.js:

function popup() {
var link = document.URL;
alert("This is the Link : ( " +link+ " )");
}

回答by HaNdTriX

Don't use inline Javascript in a chrome extension.

不要在 chrome 扩展中使用内联 Javascript。

HTML:

HTML:

<div class="plus"></div>
<script src="inline.js"></script>

JS:

JS:

function popup(e) {
  var link = document.URL;
  alert("This is the Link : ( " +link+ " )");
}

var plusBtn = document.querySelector('.plus');
plusBtn.addEventListener('click', popup);