开发者

Selector - Selecting all tables on a page in jQuery

Say I have multiple tables (no IDs or names) on开发者_开发百科 a page at various levels within embedded divs. What would my selector be (if it is possible) to select all tables regardless of where it resides on a page and iterate or filter the tables based on the content of the first cell in the first row?


You can simply use $('table') as your selector.

Then you can use the existing filters such as ":contains" or ":has", or the .filter() function if you need more finegrained control over in filtering your results. For example,

$('table:has(td > span)')

or

$('table').filter(function(index){
  return $(this).html() == "<tr><td>something</td></tr>";
});


Try...

$("table").each(function(){
  var curTable = $(this);
  var cell = $(this).find("tr:first td:first");

  if ($(cell).text() == "some text"){
  }
});

alternatively you could all check the html of the first cell in the if clause by $(cell).html()


To select all tables couldn't be simpler:

$("table")

Adding a filter

$("table:has(td:first:contains('mytext'))")


This will select all the tables:

$("table")

This will select the first TD cell of the first row of each table:

$("table tr:first td:first")


You can get every table by just using jQuery('table'). Whether the tables are in various levels or embedded within divs or whatever doesn't change.

To do additional filtering:

jQuery('table').filter( function() { ... } );

The passed in function will map the table element to this, and you would need to return true to keep it in your collection, or false to discard it.


 $('table').each(function(){
     $(this).find('tr :first')...
 });


If you are wanting to look at the first cell in the first row of each table you can use:

$("table tr:first td:first").each(function() { var html = this.innerHTML; /* Iterative logic here */ });


You should try something like $('table tr:first td:first:containts("whatever")') to grab the first cell of the first row with specific content.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜