jQuery select ancestor

后端 未结 6 1953
清酒与你
清酒与你 2020-12-24 06:44

Is is possible to use jQuery to select an ancestor of an element?

Markup:


      
      
6条回答
  • 2020-12-24 06:46

    http://api.jquery.com/parent/ and http://api.jquery.com/parents/

    $(".click-me").click(function(){
        var ancestorId = $(this).parent().parent();
        alert(ancestorId)
    });
    

    would return the divs with the ids

    0 讨论(0)
  • 2020-12-24 06:53

    It really depends what you want to achieve. Do you want to search for all ancestors regardless of class used? Or do you want to search for all elements that are ancestors and have certain class (ancestor-x in your case)?

    If you want to cycle through any ancestors, simply use .parent() (there's an excellent example how to cycle through all elements) or .parents() which can you use like:

    $(".click-me").click(function(){
        var parentElements = $(this).parents().map(function () {
            return this.tagName;
        })
        // whatever else you want to do with it
    });
    

    Probably the best approach would be to use .parents() until you get to element with certain id or class. It really depends on what you want to do.

    0 讨论(0)
  • 2020-12-24 07:00

    You mean something like this?

    $('.click-me').click(function() {
        var $theAncestor = $(this).closest('#ancestor-1');
    }
    

    This will search through all ancestors until a match is found.

    http://api.jquery.com/closest/

    EDIT:

    Jerome, your question can be interpreted several ways. This speaks to the power and flexibility of jQuery.

    Please consider the following.

    First, to answer your question, yes, it is possible to use jQuery to select an ancestor of an element.

    I think we can assume that you are aware of jQuery's ability to select any element, whether ancestor or descendant via:

    $('#myElement')
    

    Given the click-me example, if you would like to have a set of all of an element's ancestors returned, use:

    $(this).parents()
    

    or

    $(this).parents(selector)
    

    But be aware that this will traverse through ALL ancestors returning all, or all that match when a selector is given.

    If you would like to have the immediate parent returned, use:

    $(this).parent()
    

    If you know which ancestor you need, use:

    $(this).closest(selector)
    

    But be aware that it will only return the first match, and if the current element (this) is a match, it will return that.

    I hope this helps.

    0 讨论(0)
  • 2020-12-24 07:05

    Try parent() for the immediate parent element.

    $(".click-me").click(function() {
      var ancestor = $(this).parent();
      alert(ancestor)
    });
    

    Or parents() for all matching ancestor elements.

    $(".click-me").click(function() {
      var ancestors = $(this).parents(".some-ancestor");
      alert(ancestors)
    });
    

    Or closest() for the first closest matching element (either an ancestor or self).

    $(".click-me").click(function() {
      var ancestor = $(this).closest(".some-ancestor");
      alert(ancestor)
    });
    

    The difference between parents() and closest() is subtle but important. closest() will return the current element if it's a match; parents() returns only ancestors. You many not want the possibility of returning the current element. closest() also only returns one element; parents() returns all matching elements.

    0 讨论(0)
  • 2020-12-24 07:08

    Are you looking for parent()? jQuery Doc for parent selector. If it is different for your scenario explain what is your expected output.

    0 讨论(0)
  • 2020-12-24 07:09

    Try using parents() or closest() in combination, perhaps, with a selector to determine which ancestor should match. For example, find the closest ancestor div with an id.

    $('.click-me').click( function() {
          var ancestorId = $(this).closest('div[id]');
          alert(ancestorId);
    });
    
    0 讨论(0)
提交回复
热议问题