我有一个帖子索引页面。
点击给定帖子的显示评论按钮后,帖子的评论会显示。这很简单,因为我可以根据点击位置使用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');
};
};
});
_post partial(单个帖子' s 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>
答案 0 :(得分:2)
尝试遍历每个帖子。我无法测试它,但这样的事情应该有效:
$( '.post-panel' ).each(function( index ) {
if ($(this).find('.edit-post-dropdown-button').data('postauthorid') == $('#bodycurrentuser').data('currentuserid')) {
$(this).find('.edit-post-dropdown-button').removeClass('hidden');
}
});
答案 1 :(得分:1)
您实际上甚至不需要浏览网页的所有帖子,您需要选择postauthorid
=== currentuserid
的项目。
var currentUserId = $('#bodycurrentuser').data('currentuserid');
var $currentUserDropdowns = $('edit-post-dropdown-button[data-postauthorid=' + currentUserId + ']');
$.each($currentUserDropdowns, removeHiddenClass);
function removeHiddenClass() {
$(this).removeClass('hidden');
}