Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. // Please forgive the rough cut and paste and lack of comments :)
  2.  
  3. // Set $els and get $closestSharedAncestor jQuery object at the bottom
  4. $els;
  5.  
  6. // Get the closest common ancestor
  7. var $closestSharedAncestor,
  8. ancestorsArray,
  9. longestAncestorBranch,
  10. ancestorsArrayLength,
  11. ancestorDiffers,
  12. i,
  13. j;
  14.  
  15.  
  16. ancestorsArray = [];
  17. longestAncestorBranch = 0;
  18. $els.each(function() {
  19. var $ancestors,
  20. ancestorsLength;
  21.  
  22. $ancestors = $(this).parents();
  23. ancestorsLength = $ancestors.length;
  24.  
  25. if (ancestorsLength > longestAncestorBranch) {
  26. longestAncestorBranch = ancestorsLength;
  27. }
  28.  
  29. ancestorsArray.push($ancestors.get().reverse());
  30.  
  31. });
  32. ancestorsArrayLength = ancestorsArray.length;
  33.  
  34. $closestSharedAncestor = ancestorsArray[0][0];
  35. ancestorDiffers = false;
  36. for (i = 0; i < longestAncestorBranch; i++) {
  37. for (j = 0; j < ancestorsArrayLength - 1; j++) {
  38. if (ancestorsArray[j][i] !== ancestorsArray[j + 1][i]) {
  39. ancestorDiffers = true;
  40. break;
  41. }
  42. }
  43. if (ancestorDiffers) {
  44. break;
  45. }
  46. $closestSharedAncestor = ancestorsArray[0][i];
  47. }
  48. $closestSharedAncestor = $($closestSharedAncestor);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement