简体   繁体   中英

How can I combine an attribute and nth-of-type in querySelector?

I am trying to select a cell in a table by it's attribute (for which I could use querySelector("[data-name]"), but I want it to return the elements in the third column only. Is it possible to combine an attribute search with nth-of-type(2)?

I've tried using querySelector("[data-name]:nth-of-type(2)"] but that doesn't work.

for 3rd column try with nth-of-type(3)

:nth-of-type() is a css selector and index of the first child will be 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM