DOMNodeInserted事件循环 [英] DOMNodeInserted event loop
问题描述
我正在为Facebook开发Chrome扩展(我的第四个问题...),它在喜欢按钮旁添加一个自定义按钮。由于帖子会自动添加到新闻Feed中而不刷新页面,所以每次添加新帖时,都必须添加脚本。
I am developing a chrome extension (my fourth question or so...) for facebook, which adds a custom button beside the "like" button. Since posts are added automatically to the news feed without a page refresh, I have to add the script every time new posts are added.
而且我使用 DOMNodeInserted
事件
问题是当事件被调用时,我将一个新的元素(按钮)插入到页面中,一个循环!
The problem is that when the event is called I insert a new element (the button) to the page and it makes a loop!
我的脚本:
$(document).bind('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();
});
您可以看到我以前的问题 here
You can see my previous question here
我厌倦了提问,所以我会真的很感谢任何答案/评论!
I'm tired of asking questions so I'll really appreciate any answer/comment!
推荐答案
这是否有效?提供您不要插入任何更多的 .like_link
元素,当您的元素插入发生时,这应该是无操作的,因为它只会查找包含 .like_link
。
Does this work? Providing that you don't insert any more .like_link
elements, this should be a no-op when your element insertion happens, since it only looks for node insertions which contain a .like_link
.
$(document).bind('DOMNodeInserted', function(event) {
$(event.target).find(".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>'
);
$(event.target).find(".taheles_saving_message").hide();
});
这篇关于DOMNodeInserted事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!