gpt4 book ai didi

javascript - 如何在 querySelector 中组合属性和第 n 个类型?

转载 作者:行者123 更新时间:2023-12-03 05:15:21 25 4
gpt4 key购买 nike

我试图通过它的属性选择表中的一个单元格(我可以使用 querySelector("[data-name]"),但我希望它只返回第三列中的元素。是否可能将属性搜索与 nth-of-type(2) 结合起来?

我尝试过使用 querySelector("[data-name]: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>

关于javascript - 如何在 querySelector 中组合属性和第 n 个类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41626114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com