javascript - How do I select the innermost element? -


in jquery, how descend far possible html tree?

for simplicity, have 1 path going downward.

(related bonus: how find deepest element multiple downward paths?)

<html>     <table id="table0">       <tr>         <td id="cell0">           <div class"simple">  want change information </div>         </td>     </tr>     </table>   </html> 

i want change innermost html of cell named cell0 don't know names of classes inside. possible select far without knowing these names?

thanks much!

for single path find element doesn't have child nodes:

$('body *:not(:has("*"))'); 

or, in more specific case $('#cell0 *:not(:has("*"))');

for multiple paths - if there multiple equally nested nodes? solution give array of nodes highest number of ancestors.

var = $('body *:not(:has("*"))'), maxdepth=0, deepest = [];  all.each( function(){      var depth = $(this).parents().length||0;      if(depth>maxdepth){          deepest = [this];          maxdepth = depth;      }     else if(depth==maxdepth){         deepest.push(this);      } }); 

again, in situation want table cells' deepest elements, you're one-liner:

$('#table0 td *:not(:has("*"))'); 

- return jquery object containing innermost child nodes of every cell in table.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -