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

前端 未结 14 1940
-上瘾入骨i
-上瘾入骨i 2020-11-30 04:03

Users selects two or more elements in a HTML page. What i want to accomplish is to find those elements\' common ancestors (so body node would be the common ancestor if none

14条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 04:15

    You should be able to use the jQuery .parents() function and then walk through the results looking for the first match. (Or I guess you could start from the end and go backwards until you see the first difference; that's probably better.)

提交回复
热议问题