我有一个页面,用户可以在其中“喜欢”某篇文章。
当页面首次加载时,我想显示他们是否已经喜欢某篇文章。
我用来将 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 的任何地方?
如果没有,请尝试以下操作:
$(html).find('.like').show();
$(html).find('.unlike').hide();
关于javascript - jquery显示/隐藏被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14386332/