我正在尝试使用jQuery创建一个函数,以便遍历表中的每个<td>
,此时页面中有多个表没有特定的ID。
我理解如何loop through all td elements in a table,即使table id is passed,但是当由一个子<td>
元素触发的函数具有此功能时,我无法使其工作使用父表。
我尝试过(非常愚蠢)像
var tbl = $(this).closest('table');
$(tbl+" <td>").each(function() {
但是当然这根本不起作用。
我可以生成id以使事情变得更容易,但我确信有一种方法可以更优雅地使用jQuery。
答案 0 :(得分:2)
试试这个
$("td", tbl).each(function() {
答案 1 :(得分:1)
尝试此操作:获取表格的ID并将其与.each
一起使用。
var tbl = $(this).closest('table');
$(tbl).find("td").each(function() {
//do stuff here...
}
编辑:没有ID ....
答案 2 :(得分:1)
试试这个:
var tbl = $(this).closest('table');
tbl.find('td').each(function() {
var $td = $(this);//"this" keyword is current context;"td" element, so $(this) is a "TD element" jquery object.
}
答案 3 :(得分:1)
如果表中有任何元素,例如来自事件处理程序的this
,那么你可以这样做:
$(this).closest("table").find("td").each(function(index, element) {
// you will get each td in the table here
});