我有一个帖子的索引页面。jQuery的迭代收集与
单击给定帖子的显示评论按钮后,该帖子的评论可见。这很容易,因为我可以根据点击位置使用this
,然后find
。现在
//open hidden post comments and replies in post thread
$(document).on('click', '.open-all-post-comments', function (event) {
var post_id = $(this).data('pid');
var all_replies = $('#post_' + post_id).find('.post-comment-replies:has(.post-comment-reply)');
all_replies.show();
$(this).closest('.open-all-post-comments-row').hide();
});
,在页面加载,我想作出在帖子的作者是当前用户的职位编辑下拉可见。我不知道如何去通过页面上的所有帖子,检查给定的数据attr是否等于当前用户的id,然后使下拉菜单可见,如果是这样。
这是我现在的代码。我应该如何改变它以使其工作?
//checking all posts on the page and show the dropdown if user is the post author
$(document).on("page:change", function() {
if ($('.post-container').length > 0) {
if ($('.edit-post-dropdown-button').data('postauthorid') == $('#bodycurrentuser').data('currentuserid')) {
$('.edit-post-dropdown-button').removeClass('hidden');
};
};
});
彦博部分(单篇文章的HTML)
<div class="panel panel-default post-panel" id="post_<%= post.id %>">
........
<li class="dropdown edit-post-dropdown-button hidden" data-postauthorid ="<%= post.user_id%>">
......
</li>
</div>
你缺少一个'$''之前(文件)'' – callback
$ .each'功能 –
为u_mulder说,你应该使用'$ .each'功能通过你需要更多posts.If迭代详细的答案显示你的HTML,所以我们可以帮助你 –