我有一个相当简单的动态 html 结构的表,其中包含链接,并且它可能包含第二个带链接的表。我想更改主表中链接的 html 属性,但不更改第二个表中的链接。
我的结构:
<table>
<tr><td><a href="www.google.com">link</a></td></tr>
<tr><td><a href="www.google.com">link</a></td></tr>
<tr><td><a href="www.google.com">link</a></td></tr>
<tr><td><a href="www.google.com">link</a></td></tr>
<tr><td><a href="www.google.com">link</a></td></tr>
<tr>
<td>
<table>
<tr><td><a href="http://stackoverflow.com">link</a></td></tr>
<tr><td><a href="http://stackoverflow.com">link</a></td></tr>
<tr><td><a href="http://stackoverflow.com">link</a></td></tr>
</table>
</td>
</tr>
例如,我想将“www.google.com”的所有hrefs更改为“www.foo.com”。我可以更改 href 属性,但我的选择器有问题,因为有时第二个表不存在。
我当前的选择器如下所示:$('table a').filter(':not(table:last a)')
我确信这不是最有效的方法,但它一直有效,直到没有第二张 table 发挥作用为止。
最佳答案
如果您的“主”表不在另一个表内,您可以这样做:
$('table a:not(table table a)')
或等效的:
$('table a').not('table table a')
更新:性能方面,Patrick's answer好多了。
关于jQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4588832/