在元素中查找文本并将Class添加到Parent
HTML:
<table>
<tr>
<td>
<a href="#" class="nav">link</a>
<td>
</tr>
</table>
我想要:在 a.nav 中查找文本“链接”并将 ID“abc”添加到“表”。尝试了这个,但它不起作用:
$('table>tbody>tr>td>a.nav:contains("Forum Index")').parents('table').attr('id', 'newID');
(tbody,因为大多数浏览器会自动添加它)
HTML:
<table>
<tr>
<td>
<a href="#" class="nav">link</a>
<td>
</tr>
</table>
I want to: FIND TEXT 'link' in a.nav and ADD ID "abc" to 'table'. Tried this but it doesn't work:
$('table>tbody>tr>td>a.nav:contains("Forum Index")').parents('table').attr('id', 'newID');
(tbody because most browsers add it automatically)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的选择标准不需要那么明确。事实上,我会让它们尽可能简单,同时仍然只选择您需要的元素。简单的选择标准会更高效。
Your selection criteria does not need to be so explicit. In fact I would make them as simple as possible while still selecting only the elements that you need. Simple selection criteria will be more performant.
是的,它确实有效。我尝试了仅将包含中的字符串从“论坛索引”更改为“链接”,并且它确实更改了表的 id。
但是,您不应该真正更改元素的 id。如果可能的话,改用一个类。
Yes, it does work. I tried it with only changing the string in the contains from "Forum Index" to "link", and it does change the id of the table.
However, you shouldn't really change the id of elements. Use a class intead if possible.