我正在开发一个chrome扩展(我的第四个问题……)对于facebook,它在“喜欢”按钮旁边添加了一个自定义按钮。由于帖子会自动添加到新闻提要中,而无需刷新页面,因此每次添加新帖子时,我都必须添加脚本。
我使用的是DOMNodeInserted事件。
问题是,当事件被调用时,我在页面中插入了一个新元素(按钮),它会产生一个循环!
我的脚本:
$(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
我已经厌倦了问问题,所以我真的很感谢任何人的回答/评论!
发布于 2012-05-20 22:33:45
这行得通吗?假设您不再插入任何.like_link元素,那么当您的元素插入发生时,这应该是无操作的,因为它只查找包含.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();
});https://stackoverflow.com/questions/10674079
复制相似问题