Conside below html -
-
just use
$(".element").closest('#container1');
if no ancestor with that id
is found then
$(".element").closest('#container1').length
will be 0
讨论(0)
-
$(".element").parents();
will give all parents of .element
(including html
and body
)
DEMO
To find any specific parent, suppose container1
then
$('.element').parents('.container1')
DEMO
jQuery .parents() generally find all parents, but if you passed a selector then it will search for that.
讨论(0)
-
To get the first parent personally I use the following construction:
var count_parents = $(".element").parents().length;
$(".element").parents().eq(count_parents - 1);
Hope, it will be helpful for someone.
讨论(0)
- 热议问题