简体   繁体   English

如何在 Chrome 扩展程序中创建“onclick”事件?

[英]How can I create "onclick" event in Chrome extension?

Here is my main.html file:这是我的main.html文件:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<meta name="generator" content="WYSIWYG Web Builder 16 - https://www.wysiwygwebbuilder.com">
<link href="Chromium_Güncelleştirici_Eklenti_Projesi.css" rel="stylesheet">
<link href="main.css" rel="stylesheet">
</head>
<body>
<div id="wb_Image1" style="left:0px;top:0px;width:320px;height:50px;z-index:0;">
<img src="Assets/baslik.png" id="Image1" alt=""></div>
<div id="wb_Image7" style="left:0px;top:160px;width:320px;height:35px;z-index:1;">
<img src="Assets/guncellestirmeleriDenetlePasif.png" id="Image2" alt=""></div>
<div id="wb_Image3" style="left:0px;top:87px;width:320px;height:36px;z-index:3;">
<img src="Assets/ayarlarPasif.png" id="Image3" alt=""></div>
<div id="wb_Image4" style="left:0px;top:123px;width:320px;height:72px;z-index:4;">
<img src="Assets/hakkindaPasif.png" id="Image4" alt=""></div>
<script src="../Data/deneme.js" type="module">
</body>
</html>

Here is my deneme.js file:这是我的deneme.js文件:

document.addEventListener
(
    'DOMContentLoaded',
    function()
    {
        var dugme = document.getElementById('Image2');
        
        dugme.addEventListener
        (
            'click',
            function()
            {
                console.log('Merhaba Dünya!');
            }
        );
    }
);

But the problem is, when I click the Image2 , the browser does not respond.但问题是,当我单击Image2时,浏览器没有响应。

I did not write JS code into my main.html file;我没有将 JS 代码写入我的main.html文件; because Chromium blocks my extension (Content Security Policy).因为 Chromium 阻止了我的扩展(内容安全策略)。

So, what should I do?所以我该怎么做?

You didn't close the script tag.您没有关闭脚本标签。

Try below html, it should work fine:试试下面的 html,它应该可以正常工作:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<meta name="generator" content="WYSIWYG Web Builder 16 - https://www.wysiwygwebbuilder.com">
<link href="Chromium_Güncelleştirici_Eklenti_Projesi.css" rel="stylesheet">
<link href="main.css" rel="stylesheet">
</head>
<body>
<div id="wb_Image1" style="left:0px;top:0px;width:320px;height:50px;z-index:0;">
<img src="Assets/baslik.png" id="Image1" alt=""></div>
<div id="wb_Image7" style="left:0px;top:160px;width:320px;height:35px;z-index:1;">
<img src="Assets/guncellestirmeleriDenetlePasif.png" id="Image2" alt=""></div>
<div id="wb_Image3" style="left:0px;top:87px;width:320px;height:36px;z-index:3;">
<img src="Assets/ayarlarPasif.png" id="Image3" alt=""></div>
<div id="wb_Image4" style="left:0px;top:123px;width:320px;height:72px;z-index:4;">
<img src="Assets/hakkindaPasif.png" id="Image4" alt=""></div>
<script src="../Data/deneme.js" type="module"></script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM