如何从jquery选择器中排除单击的项及其所有父项?

时间:2014-05-19 11:44:35

标签: jquery

在我的jquery脚本中,我试图从选择器中排除一个项及其所有父项:

$(".Navigation-list--dropdown").not($(this).parent()).addClass('is-hidden');

但是它没有用,有谁知道为什么?

这是完整的脚本:

$(document).ready(function() {
  // When a dropdown trigger is clicked
  $('.Navigation-link--dropdownTrigger').click(function(e) {
    // If its sibling dropdown list is hidden
    if( $(this).siblings('.Navigation-list--dropdown').hasClass('is-hidden') ){
      // Hide all other dropdown lists, except the clicked list and its parents
      $(".Navigation-list--dropdown").not($(this).parent()).addClass('is-hidden');
      // unhide only the dropdown whose trigger was clicked
      $(this).siblings('.Navigation-list--dropdown').removeClass('is-hidden'); 
    }
  });

1 个答案:

答案 0 :(得分:0)

如果要排除this及其所有父级,请先调用parents()以获取完整的祖先链,然后应用addBack()添加原始元素(即{{1} }})回到集合中:

this