[英]How can I combine an attribute and nth-of-type in querySelector?
我正在嘗試通過屬性選擇表中的單元格(可以使用querySelector(“ [data-name]”),但是我希望它僅返回第三列中的元素。是否可以合並一個nth-of-type(2)進行屬性搜索?
我試過使用querySelector(“ [數據名稱]:nth-of-type(2)”],但這不起作用。
對於第三列,請嘗試使用nth-of-type(3)
:nth-of-type()是一個CSS選擇器,第一個孩子的索引將是1
http://www.w3schools.com/cssref/sel_nth-of-type.asp
console.log(document.querySelector("table tr [data-name]:nth-of-type(3)"));
<table> <tr> <td data-name="1">1</td> <td data-name="2">2</td> <td data-name="3">3</td> <tr> </table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.