开发者

get a common parent for two DOM elements [duplicate]

开发者 https://www.devze.com 2023-04-10 19:17 出处:网络
This question already has answers here: 开发者_Go百科Closed 11 years ago. Possible Duplicate: How to find the nearest common ancestors of two or more nodes?
This question already has answers here: 开发者_Go百科Closed 11 years ago.

Possible Duplicate:

How to find the nearest common ancestors of two or more nodes?

I have 2 anchor elements and i need to get the common parent of 2 of them.

Some times they are in a ul, sometimes in a table, or any other way.

I am doing this:

var idParent = $(elem1).parents().filter($(elem2).parents()).first().attr('id');

But it doesnt always work, i finds it element sometimes...

Any better idea? or a way to fix this?

EDIT: I want a jquery solution.

Thank you


var parent = $(elem1).parents().has(elem2).first();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号