2015-12-22 15 views
-2
$(document.body).on("click",'.sub-unfollow', function(){ 
    var unfollow_tag = {element:"",un:"",type:"",text:""}; 
    var unfollow_tag.element = $(this).parents("li"); 
    var unfollow_tag.un = $(this).parents("li").attr("data-un"); 
    var unfollow_tag.type = $(this).parents("li").attr("data-type"); 
    var unfollow_tag.text = $(this).parents("li").text(); 
    alert(unfollow_tag.text); 
}); 

使用此看似基本的對象設置獲取錯誤。有任何想法嗎?JavaScript對象使用點符號的錯誤

+0

你得到的錯誤是什麼? – Tushar

+0

「出錯」,出現什麼錯誤? –

+0

實際上並不確定,但是當這段代碼存在時,我的頁面上沒有運行js。它只在我刪除所有對unfollow_tag對象的引用時纔開始工作 –

回答

0

您應該在聲明unfollow_tag後刪除var,它會重新聲明它而不是嘗試訪問屬性。

$(document).on("click",'.sub-unfollow', function(){ 
    var unfollow_tag = {}; 
    unfollow_tag.element = $(this).parents("li"); 
    unfollow_tag.un = $(this).parents("li").attr("data-un"); 
    unfollow_tag.type = $(this).parents("li").attr("data-type"); 
    unfollow_tag.text = $(this).parents("li").text(); 
    alert(unfollow_tag.text); 
});