Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Please forgive the rough cut and paste and lack of comments :)
- // Set $els and get $closestSharedAncestor jQuery object at the bottom
- $els;
- // Get the closest common ancestor
- var $closestSharedAncestor,
- ancestorsArray,
- longestAncestorBranch,
- ancestorsArrayLength,
- ancestorDiffers,
- i,
- j;
- ancestorsArray = [];
- longestAncestorBranch = 0;
- $els.each(function() {
- var $ancestors,
- ancestorsLength;
- $ancestors = $(this).parents();
- ancestorsLength = $ancestors.length;
- if (ancestorsLength > longestAncestorBranch) {
- longestAncestorBranch = ancestorsLength;
- }
- ancestorsArray.push($ancestors.get().reverse());
- });
- ancestorsArrayLength = ancestorsArray.length;
- $closestSharedAncestor = ancestorsArray[0][0];
- ancestorDiffers = false;
- for (i = 0; i < longestAncestorBranch; i++) {
- for (j = 0; j < ancestorsArrayLength - 1; j++) {
- if (ancestorsArray[j][i] !== ancestorsArray[j + 1][i]) {
- ancestorDiffers = true;
- break;
- }
- }
- if (ancestorDiffers) {
- break;
- }
- $closestSharedAncestor = ancestorsArray[0][i];
- }
- $closestSharedAncestor = $($closestSharedAncestor);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement