我想選擇img不包含在table. 下面的代碼應該只回傳 1 的長度,因為只有一個img標簽不在table. 注意:不能更改 html 布局或 CSS,因為有許多舊博客文章具有這種結構。
alert(document.querySelectorAll('img').length)
<table align="center">
<tbody>
<tr>
<td style="text-align: center;">
<a href="https://xxx" imageanchor="1" style="margin-left: auto; margin-right: auto;">
<img border="0" src="https://xxx" original="https://xxx" style="">
</a>
</td>
</tr>
<tr>
<td class="tr-caption" style="text-align: center;">Double Trailing</td>
</tr>
</tbody>
</table>
<a href="https://xxx" imageanchor="1" style="margin-left: auto; margin-right: auto;">
<img border="0" src="https://xxx" original="https://yyy" style="">
</a>
uj5u.com熱心網友回復:
要定位不滿足給定條件的元素,可以使用:not()CSS 中的偽類:
console.log(document.querySelectorAll('img:not(table img)').length)
<table align="center">
<tbody>
<tr>
<td style="text-align: center;">
<a href="https://xxx" imageanchor="1" style="margin-left: auto; margin-right: auto;">
<img border="0" src="https://xxx" original="https://xxx" style="">
</a>
</td>
</tr>
<tr>
<td class="tr-caption" style="text-align: center;">Double Trailing</td>
</tr>
</tbody>
</table>
<a href="https://xxx" imageanchor="1" style="margin-left: auto; margin-right: auto;">
<img border="0" src="https://xxx" original="https://yyy" style="">
</a>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/456289.html
標籤:javascript html css
下一篇:基于兩個布爾條件的js陣列過濾器
