1

我正在開發一個chrome擴展(我的第四個問題左右......),它在「like」按鈕旁邊添加了一個自定義按鈕。由於帖子自動添加到新聞Feed中而不刷新頁面,因此每次添加新帖子時都必須添加腳本。DOMNodeInserted事件循環

而我用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(); 
}); 

你可以看到我剛纔的問題here

我已經厭倦了問問題,所以我會很感激任何答案/發表評論!

+0

你可能想在這裏使用[的liveQuery(http://docs.jquery.com/Plugins/livequery)插件。然後再次,似乎沒有使用這個事件。 – Eric

回答

1

這是行不通的嗎?假如你不再插入任何.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(); 
}); 
+0

非常感謝。 –