jQuery select ancestor

我的未来我决定 提交于 2019-11-29 22:57:27

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.

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.

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);
});

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!