jQuery选择器可以应用于元素而不是整个文档吗?

Flo*_*rin 8 jquery css-selectors

jQuery('td[class=bgoff]').each(function() {
    var td = jQuery(this);
    ... no apply selector to "this" only
});
Run Code Online (Sandbox Code Playgroud)

我正在使用html中的表格数据并尝试解析每个TD的内容(它们不是唯一可识别的).

使用XPath,我可以将"this"的路径添加到其他选择中.

我怎样才能用jQuery实现这一目标?

Sco*_*den 24

使用jQuery,您可以选择在选择器表达式之后提供第二个参数,这将成为jQuery用于限制查找范围的上下文.在这里了解更多


Joe*_*oel 15

如果您已经有一个想要搜索的jquery对象,也可以使用.find(expression).

在你的例子中:

jQuery('td[class=bgoff]').each(function() {
    var td = jQuery(this);
    $(td).find( <selector to search within td> );
});
Run Code Online (Sandbox Code Playgroud)