[英]Why my Chrome extension event doesn't work?
I'm trying to create a chrome extension.我正在尝试创建一个 chrome 扩展。 I had a problem with the affectation of event for the new element that i append to the dom of site with content.
我对添加到带有内容的站点 dom 的新元素的事件影响有问题。 Js
JS
If I add an event to an element' 'for example class' exist already in the page, it works correctly.如果我向页面中已经存在的元素“例如类”添加事件,则它可以正常工作。 Just for my new appended element((in the code iadded a button ,the event is just an alert to test))
仅针对我的新附加元素((在代码中添加了一个按钮,该事件只是要测试的警报))
function tst() {
myclass = $("._3hg-._42ft");
myclass = myclass.not(".supp");
myclass.addClass("supp");
var patt = /https:\/\/(.)*\.facebook\.com\/(.)*\/(posts|photos|videos)\/(\w|\.|\d)*/g;
for (i = 0; i < myclass.length; i++) {
result = patt.exec(myclass[i]);
myclass.append('<button class="fact" id=' + result[0] + ' style="position: absolute;">fact</button>');
};
/* this is a simple event*/
/***********************/
$(".fact").on('click', function() {
alert("no event work ");
});
Making somewhat broad assumption here in my answer that it is JavaScript/jQuery related and is NOT an extension...or is so still in that context.在我的回答中做出了一些广泛的假设,即它与 JavaScript/jQuery 相关并且不是扩展......或者仍然在那个上下文中。 You need to attach the event to the container here perhaps for the dynamically created elements.
您需要将事件附加到此处的容器,也许是为了动态创建的元素。 Lots of global stuff, suggested to not do that, updated there.
许多全球性的东西,建议不要这样做,在那里更新。
Appends a lot of buttons perhaps?可能会附加很多按钮? might need to only hit DOM once but left as-is in this isolated function.
可能只需要点击 DOM 一次,但在这个孤立的函数中保持原样。
function tst() { let myclass = $("._3hg-._42ft") .not(".supp"); myclass.addClass("supp"); //let result = {}; var patt = /https:\\/\\/(.)*\\.facebook\\.com\\/(.)*\\/(posts|photos|videos)\\/(\\w|\\.|\\d)*/g; var i = 0; //avoid global for (i; i < myclass.length; i++) { // broad assumption of the returned value from patt.exec() here // not even sure why it needs an id, have a class, use for css let result = patt.exec(myclass[i]); myclass.append('<button class="fact" id="' + result[0] + '">fact</button>'); } /* attache event to pre-existing element */ /***********************/ myclass.on('click', ".fact", function() { alert("event works"); }); }
button.fact { position: absolute; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.