按钮不适用于自定义Chrome扩展程序

时间:2016-04-18 04:19:42

标签: javascript imgur

我尝试制作一个显示日期的Chrome扩展程序,一切顺利,除非我尝试运行它。我点击了我编码的按钮,它不会显示我想要的内容。

代码:

JS



function myFunction() {
    var d = new Date();
    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    var n = weekday[d.getDay()];
    document.getElementById("checkday").innerHTML = n;



 的 HTML



<!doctype html>
<html>

<head>
  <title>Day checker</title>
  <script src="popup.js"></script>
</head>

<body>
  <h1>Click to show day</h1>
  <button id="checkday">Check!</button>
</body>

</html>
&#13;
&#13;
&#13;

JSON

{
    "manifest_version": 2,

    "name": "What day is it",
    "description": "This extension will find the date",
    "version": "1.0",

    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "permissions": [
        "activeTab"
    ]
}

图片(19×19) http://i.stack.imgur.com/FM9wt.png

1 个答案:

答案 0 :(得分:1)

在加载DOM内容后,必须动态链接动作。 出于安全原因,静态链接的操作(例如onClick属性)不适用于Chrome扩展程序。

在你的js文件(popup.js)中,添加这个

document.addEventListener('DOMContentLoaded', function(){
  document.querySelector('#checkday').addEventListener('click', myFunction);
  // anything else you want to initialize on the page
});

这应该让它发挥作用。