简体   繁体   English

如何在Chrome扩展程序中使用jquery进行点击事件?

[英]How to do click events using jquery in a chrome extension?

Right now I am appending a div with the class button to the body of popup.html, and then in the popup.js I am using jQuery to do an click event, but it is not working. 现在,我将带有类按钮的div附加到popup.html的正文中,然后在popup.js中,我使用jQuery来执行click事件,但是它不起作用。 It works when I put the code in jsfiddle . 当我将代码放在jsfiddle中时,它可以工作。 Here is the HTML that I am appending to the body: 这是我要附加到正文的HTML:

<div class="button">Clear Saved Login</div>

Here is the jQuery that I use to try to get something to happen when it gets clicked on: 这是我用来尝试使它在单击时发生的jQuery:

$('document').ready(function(){
   $('.button').click(function() {
       alert('worked');
   });
});

I know that jQuery is being imported because if I put an alert right about the document.ready() it will alert me. 我知道正在导入jQuery,因为如果我对document.ready()发出警告,它将警告我。 I don't know if it is a chrome thing or I am missing something. 我不知道这是镀铬的东西还是我缺少的东西。

I had a similar issue. 我有一个类似的问题。 I had to make sure I downloaded the JQuery library into a file called jquery.js that I put in the folder of my plugin. 我必须确保将JQuery库下载到名为jquery.js的文件中,该文件放置在插件的文件夹中。 Then in your manifest include: 然后在清单中包括:

  "content_scripts": [ {
    "js": [ "jquery.js", "yourscript.js" ]
  }]

Of course yourscript.js is if you have one. 如果您有一个,那么yourscript.js当然是。 I think including it like a file like that is what did it for me if I remember correctly. 我认为,如果我没记错的话,将它像这样的文件包含在内对我来说是有用的。

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

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