我有一個帖子的索引頁面。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,所以我們可以幫助你 –