我正在构建一个功能,告诉读者他们已经阅读了特定的帖子。要做到这一点,我需要通知用户,我通过将一个元素添加到.article来完成此操作。我遇到的问题是,因为我在.each循环内附加了元素,所以元素追加了4次。在每个循环内追加一次
继承人我的代码:
$('.article-container').each(function() {
// CHECK IF THE DATA IS IN THE ARRAY
if(cookieValue.indexOf($(this).data('id')) != -1) {
$(this).addClass('readit');
$('<h4 class="readit-alert">You've read it!</h4>').appendTo('.article-container.readit');
} else {
// OTHERWISE, ADD CLASS NOT READ
$(this).addClass('notread');
}
});
这里就是它的返回:
<h4 class="readit-alert">You've read it!</h4>
<h4 class="readit-alert">You've read it!</h4>
<h4 class="readit-alert">You've read it!</h4>
<h4 class="readit-alert">You've read it!</h4>
怎么可以这样写,只追加“readit警报”只有一次的文章容器内?
在.each()回调中返回false将停止循环继续。 – dqhendricks