jquery 包含 |仅针对第一个祖先
我正在尝试在包含许多测试表的 HTML 页面中定位并最终设置字符串(在本例中为 BLA)的样式。
例如 HTML 页面:
<table>
<tr>
<td><table><div><tr><td>BLA</td></tr></div></table></td>
</tr>
和我使用的 jquery 脚本:
$("td:contains('BLA')");
来执行此操作,但我看到所有 td 都被选中..我怎样才能获得第一个 td,而不需要所有祖先?.. 如果这是可能的,我如何在 DOM 树上向第一个祖先添加一个类? (只有这个)
谢谢社区
I am trying to target and eventually style a string, in this examle BLA, in an HTML page with many tested tables.
For example HTML page:
<table>
<tr>
<td><table><div><tr><td>BLA</td></tr></div></table></td>
</tr>
And the jquery script I use:
$("td:contains('BLA')");
to do it, but I see that ALL td's are selected.. How can I just get the first td, without all the ancestors?..
And if this is possible, how can I add a class to the first ancestor when going up the DOM tree? (and just this one)
Thank you community
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的是最后一个 td,而不是第一个,因为它们是按 DOM 层次结构顺序返回的。
演示位于http://jsfiddle.net/92Xj2/
更新之后评论中的说明
新演示 http://jsfiddle.net/加比/92Xj2/4/
What you describe is the last td and not the first since they are returned in DOM hierarchy order.
demo at http://jsfiddle.net/92Xj2/
Update after clarification in comment
new demo http://jsfiddle.net/gaby/92Xj2/4/