jQuery select ancestor

雨燕双飞 提交于 2019-12-18 10:40:56

问题


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

Markup:

<div id="ancestor-1">
    <div>
        <a href="#" class="click-me">Click me</a>
    </div>
</div>
<div id="ancestor-2">
    <div>
        <a href="#" class="click-me">Click me</a>
    </div>
</div>

Script:

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

回答1:


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.




回答2:


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.




回答3:


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



回答4:


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




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/2200775/jquery-select-ancestor

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