Javascript 选择器 - 在 jQuery 中选择页面上的所有表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1889433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selector - Selecting all tables on a page in jQuery
提问by GregH
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?
假设我在嵌入 div 的各个级别的页面上有多个表(没有 ID 或名称)。我的选择器是什么(如果可能)选择所有表格,而不管它驻留在页面上的哪个位置,并根据第一行中第一个单元格的内容迭代或过滤表格?
回答by womp
You can simply use $('table')as your selector.
您可以简单地$('table')用作选择器。
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,
然后,您可以使用现有的过滤器,例如“ :contains”或“ :has”,或者.filter()如果您需要对过滤结果进行更细粒度的控制,则可以使用该函数。例如,
$('table:has(td > span)')
or
或者
$('table').filter(function(index){
return $(this).html() == "<tr><td>something</td></tr>";
});
回答by Ryan
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()
或者,您都可以通过 $(cell).html() 检查 if 子句中第一个单元格的 html
回答by Tinister
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.
您只需使用jQuery('table'). 表格是否处于不同级别或嵌入在 div 中或其他任何内容都不会改变。
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 元素映射到this,您需要返回 true 以将其保留在您的集合中,或者返回 false 以丢弃它。
回答by Dan
To select all tables couldn't be simpler:
选择所有表再简单不过了:
$("table")
Adding a filter
添加过滤器
$("table:has(td:first:contains('mytext'))")
回答by David M
This will select all the tables:
这将选择所有表:
$("table")
This will select the first TD cell of the first row of each table:
这将选择每个表格第一行的第一个 TD 单元格:
$("table tr:first td:first")
回答by Dave Bower
$('table').each(function(){
$(this).find('tr :first')...
});
回答by Justin Swartsel
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 */
});
$("table tr:first td:first").each(function() {
var html = this.innerHTML;
/* Iterative logic here */
});
回答by Pike65
You should try something like $('table tr:first td:first:containts("whatever")')to grab the first cell of the first row with specific content.
您应该尝试$('table tr:first td:first:containts("whatever")')使用特定内容抓取第一行的第一个单元格。

