【问题标题】:Chrome extension: onclick() event is not triggering an alert() popup [duplicate]Chrome 扩展:onclick() 事件未触发 alert() 弹出窗口 [重复]
【发布时间】:2013-03-23 21:18:52
【问题描述】:

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

代码

文件 manifest.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>
<head></head>

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

文件 inline.js

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

【问题讨论】:

  • 不要使用任何内联代码,将其全部移动到外部文件中。这也包括onclick
  • @BeardFist 非常感谢。以及如何获取网站的网址?它只是显示 popup.html 的 url
  • 你可以使用chrome.tabs.queryactive:true获取当前标签,然后使用tab.url获取url。

标签: javascript google-chrome-extension event-handling dom-events google-chrome-app


【解决方案1】:

不要在 Google Chrome 扩展程序中使用内联 JavaScript 代码。

HTML:

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

JavaScript:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2013-07-10
    相关资源
    最近更新 更多