如何在jQuery中选择这个的后代?

时间:2016-06-01 13:36:22

标签: javascript 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
});
&#13;
&#13;
&#13;

请建议语法正确的选择器

2 个答案:

答案 0 :(得分:1)

通过this as second parameter to set the context

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

<小时/> 或者使用 $(this).find('td') 方法,在这种情况下,您可以使用 end() 方法将引用返回$(this)

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

答案 1 :(得分:0)

jQuery有很多方法可以通过lmtool函数或仅使用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){ ... });
相关问题