我有一個頁面,用戶可以'喜歡'一篇文章。jquery顯示/隱藏被忽略
頁面第一次加載時我想顯示它們是否已經喜歡文章。
我使用HTML DOM添加的代碼是:
html += '<div class="gl_like">';
html += '<a href="#" class="like" id="'+image.article_id+'"><div class="bLike" title="Like this article"></div></a>';
html += '<a href="#" class="unlike" id="'+image.article_id+'"><div class="bUnlike" title="Unlike this article"></div></a>';
html += '</div>';
現在我檢查一下API拋出回以一個用戶是否已經喜歡的文章
if(image.numLikes<1){
$('.like').show();
$('.unlike').hide();
html += 'wooo'; // to test the code works, it does
} else {
$('.like').hide();
$('.unlike').show();
}
「wooo」被添加到html中,但顯示/隱藏功能被忽略。每個文章都會顯示「.like」類。
我想要發生的是「.like」類顯示如果用戶不喜歡文章和「.unlike」類顯示,如果他們有。
我在這裏錯過了什麼嗎?
您需要在將HTML添加到DOM之後運行if語句 – ManseUK
您是否總是「喜歡」和「不像」? – Wolf
@ManseUK可能有15篇文章在頁面上,html是從for循環加載的,therefire在添加到html後無法運行 –