简体   繁体   English

jQuery选择所有有条件的子类

[英]jquery selecting all the children classes with conditional

I would like to find all the .post-comment-replies that have '.post-comment-reply' nested in them (not first level). 我想找到所有嵌套了'.post-comment-reply' (而不是第一级)的.post-comment-replies

With the code below I also get those .post-comment-replies that have no .post-comment-reply nested in them. 通过下面的代码,我还获得了其中没有嵌套.post-comment-reply .post-comment-replies

$(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');
  all_replies.show();
  $(this).closest('.open-all-post-comments-row').hide();
});

You can do it with :has() selector, 您可以使用:has()选择器

var all_replies = $('#post_' + post_id)
                   .find('.post-comment-replies:has(.post-comment-reply)');

The :has() selector here will assist us to get the .post-comment-replies which has one or more descendant .post-comment-reply . 这里的:has()选择器将帮助我们获取具有一个或多个后代.post-comment-reply.post-comment-replies

And if you want to make the immediate child to escape from the selector, then you can use, 如果您想让直子逃离选择器,则可以使用,

var all_replies = $('#post_' + post_id)
                   .find('.post-comment-replies > :has(.post-comment-reply)');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM