javascript 当“DOMNodeInserted”事件被调用时添加一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10667935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add an element when "DOMNodeInserted" event called
提问by Nadav S.
I want to add an element after every "like" button (chrome extension). Since posts are added to the news feed without refreshing the page, I have to add an event listener "DOMNodeInserted
". But when I try to put the after()
function inside it, it doesn't work.
我想在每个“喜欢”按钮(chrome 扩展)之后添加一个元素。由于帖子被添加到新闻提要而不刷新页面,我必须添加一个事件侦听器“ DOMNodeInserted
”。但是当我尝试将after()
函数放入其中时,它不起作用。
Code:
代码:
$("#contentArea").addEventListener("DOMNodeInserted", function(event) {
$(".like_link").after('<span class="dot"> · </span><button class="taheles_link stat_elem as_link" title="???? ???´?" type="submit" name="taheles" onclick="apply_taheles()" data-ft="{"tn":">","type":22}"><span class="taheles_default_message">???´?</span><span class="taheles_saving_message">?? ????</span></button>');
$(".taheles_saving_message").hide();
});
When I change $("#contentArea")
to document
it crashes all the page.
当我更改$("#contentArea")
为document
它时,所有页面都会崩溃。
回答by balexandre
if you want to add an event listener in jQuery, that is not the method, the corect way should always be:
如果你想在 jQuery 中添加一个事件监听器,那不是方法,正确的方法应该总是:
$(document or window).bind( your_event_name, function(event) { your_callback });
with this in mind, you can write:
考虑到这一点,你可以写:
$(document).bind('DOMNodeInserted', function(event) {
alert('inserted ' + event.target.nodeName + // new node
' in ' + event.relatedNode.nodeName); // parent
});
or do what you need to perform every time a node is inserted in the DOM.
或者每次在 DOM 中插入节点时执行您需要执行的操作。
for your information, if you want to use addEventListener
you need to use plain javascript and not jQuery.
供您参考,如果您想使用,addEventListener
您需要使用纯 javascript 而不是 jQuery。
document.addEventListener("DOMNodeInserted", function () {
// your code
}, false);
Edit:As on jQuery 1.7, please use the '.on' function instead: http://api.jquery.com/on/
编辑:与 jQuery 1.7 一样,请改用“.on”函数:http://api.jquery.com/on/
another error that comes up of your method (if it worked) is that you will break your application as you are inserting a nodeand call the DOMNodeInserted
(cause you have inserted a node) and that will insert the node again, and call the DOMNodeInserted
again...
您的方法出现的另一个错误(如果它有效)是您将在插入节点时破坏应用程序并调用DOMNodeInserted
(因为您插入了一个节点),这将再次插入节点,并DOMNodeInserted
再次调用。 ..
you can see what is going on ...
你可以看到发生了什么......
I would suggest that you listen to the correct method and append your span
there...
我建议您听听正确的方法并在span
那里附加您的...
回答by sanj singh
if you are facing problem that your code under runs only once in jquery .on() function then your should put it under addEventListner like below example //
如果您遇到的问题是您的代码在 jquery .on() 函数中只运行一次,那么您应该将它放在 addEventListner 下,如下例所示 //
document.addEventListener('DOMNodeInserted', function(e) {
$("selector").on('DOMNodeInserted', function(e) {
//your code which you want to execute each time DOMNodeInserted
});
});