簡體   English   中英

DOMNodeInserted事件循環

[英]DOMNodeInserted event loop

我正在為Facebook開發一個chrome擴展程序(我的第四個問題左右),該擴展程序在“喜歡”按鈕旁邊添加了一個自定義按鈕。 由於帖子是自動添加到新聞源的,而無需刷新頁面,因此每次添加新帖子時,我都必須添加腳本。

而且我使用DOMNodeInserted事件。

問題是,當事件被調用時,我在頁面中插入了一個新元素(按鈕),並形成了循環!

我的劇本:

$(document).bind('DOMNodeInserted', function(event) {
    $(".like_link").after('<span class="dot"> · </span><button class="taheles_link stat_elem as_link" title="תגיד תכל&acute;ס" type="submit" name="taheles" onclick="apply_taheles()" data-ft="{&quot;tn&quot;:&quot;&gt;&quot;,&quot;type&quot;:22}"><span class="taheles_default_message">תכל&acute;ס</span><span class="taheles_saving_message">לא תכלס</span></button>');
    $(".taheles_saving_message").hide();
});

你可以在這里看到我以前的問題

我已經厭倦了提出問題,所以我將非常感謝您的任何回答/評論!

這樣行嗎? 假設您不再插入.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="תגיד תכל&acute;ס" type="submit" name="taheles" onclick="apply_taheles()" data-ft="{&quot;tn&quot;:&quot;&gt;&quot;,&quot;type&quot;:22}">' +
            '<span class="taheles_default_message">תכל&acute;ס</span><span class="taheles_saving_message">לא תכלס</span>' +
        '</button>'
    );
    $(event.target).find(".taheles_saving_message").hide();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM