jquery get father of element

后端 未结 6 2040
攒了一身酷
攒了一身酷 2020-12-21 06:08

I\'ve got an element which resides inside a div. I\'d like to get the div by the child\'s ID, is this possible? if so how?

Thank you!

相关标签:
6条回答
  • 2020-12-21 06:20

    If you are not looking for the immediate parent, you can also use:

    $('#childID').closest("tr");
    

    Where tr is a selector for the parent you're trying to find. This is useful if you aren't sure of the depth of the hierarchy.

    0 讨论(0)
  • 2020-12-21 06:28

    Use the parent method of jQuery:

    $("#myChild").parent()
    

    You can even give it a selector if the parent element you're looking for is not the direct parent:

    $("#myChild").parent(".container")
    

    There's a slew of functions that help you traverse the DOM, and you can find detailed information in the jQuery reference.

    0 讨论(0)
  • 2020-12-21 06:32

    Use jQuery to get the child element, and jQuery to address the parent

    element = $('#childID').parent();
    

    Note: Yes, this is similar to Pekka's answer. It was meant to be humorous in that regard. If you don't find it humorous.....

    0 讨论(0)
  • 2020-12-21 06:39

    Try this:

    $("#id").parent()
    

    My first suggestion #id:parent doesn’t work as :parent selects all nodes that are parent of other nodes (including text nodes).

    0 讨论(0)
  • 2020-12-21 06:43

    I agree:

      $("#id").parent()
    

    Is the best method. I had this issue the other day, but was quickly resolved. I realize this was already answered but I am new. And if I had been awake! pow pow! the answer would have flown out. I'd appreciate an upvote

    BTW: here is a great new reference for jQuery http://jqapi.com/

    0 讨论(0)
  • 2020-12-21 06:47

    Using JQuery to get the child element, and standard JS to address the parent:

    element = $('#childID')[0].parentNode
    
    0 讨论(0)
提交回复
热议问题