Javascript jquery使用

Javascript jquery使用,javascript,jquery,Javascript,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 = $('

我有一个帖子索引页

单击给定帖子的“显示评论”按钮,该帖子的评论将可见。这很容易,因为我可以使用
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();
});
现在,在页面加载时,我想让编辑下拉列表对于文章作者是当前用户的文章可见。我不知道如何浏览页面上的所有帖子,检查给定的数据属性是否等于当前用户的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)


........

  • 尝试迭代每一篇文章。我无法测试它,但类似的东西应该可以工作:

    $( '.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');
               }
    });
    

    实际上,您甚至不需要浏览页面的所有帖子,您需要选择
    postauthorid
    ==
    currentuserid
    所在的项目

    var currentUserId = $('#bodycurrentuser').data('currentuserid');
    var $currentUserDropdowns = $('edit-post-dropdown-button[data-postauthorid=' + currentUserId + ']');
    
    $.each($currentUserDropdowns, removeHiddenClass);
    
    function removeHiddenClass() {
        $(this).removeClass('hidden');
    }
    

    你在
    (文档)
    之前缺少了一个
    $
    ,正如u_mulder所说,你应该使用
    $。每个
    函数都可以循环浏览帖子。如果你需要更详细的答案,请显示你的html,这样我们可以帮助你George,用html更新。这属于一个职位。我想这已经足够了,或者我应该分享更多吗?Thx George,我成功了。Francesco,这一个不起作用,因为同一页面上有更多来自同一用户的帖子。还是我错过了什么?好吧,那你是对的,但是扩展到更多的帖子很容易,我会为你更新的。汉克斯·弗朗西斯科,迫不及待地想看!我猜是打字错误(多余的烟斗)。你能为其他可能看到这篇文章的人删除它吗?请同时查看我之前的评论。Francesco,与George Pant的解决方案相比,哪一个更好/更快等。?
    
    var currentUserId = $('#bodycurrentuser').data('currentuserid');
    var $currentUserDropdowns = $('edit-post-dropdown-button[data-postauthorid=' + currentUserId + ']');
    
    $.each($currentUserDropdowns, removeHiddenClass);
    
    function removeHiddenClass() {
        $(this).removeClass('hidden');
    }