如何在jQuery中选择它的后代



>CODE:

$(this).html("<table><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr></table>");
$(this "td").each(function(index){  //i want a selector somewhat like this (to select td descendants if this)
  //mycode
});

请建议一个语法正确的选择器

传递

this作为第二个参数来设置上下文

$("td",this).each(function(index){  //i want a selector somewhat like this (to select td descendants if this)
  //mycode
});


或者使用$(this).find('td')方法,这种情况 yo 可以使用end()方法获取$(this)参考。
$(this).find("td").each(function(index){  //i want a selector somewhat like this (to select td descendants if this)
  //mycode
});

jQuery有很多方法可以通过find()函数或简单地使用普通选择器的上下文参数来定位特定元素下方出现的所有子元素:

// This will use the find() function to find all <td> elements under this element
$(this).find('td').each(function(index){ ... });
// This will do the same thing (selecting all <td> elements with this as the context)
$('td',this).each(function(index){ ... });

最新更新