gpt4 book ai didi

javascript - 如何找到 DOM 中具有 z-index 值的所有元素

转载 作者:行者123 更新时间:2023-12-03 00:21:09 26 4
gpt4 key购买 nike

我的代码中有一个表,其中包含很多行,每行都包含一些 td,我想找到所有具有 z-index 值的 td。

例如 -

<table>
<thead>
<tr>
<td>name</td>
</tr>
</thead>
<tbody>
<tr>
<td style="z-index: 10">jhon</td>
</tr>
<tr>
<td>
doy
</td>
</tr>
<tr>
<td style="z-index: 20">jam</td>
</tr>
</tbody>
</table>

有人可以帮助我在不使用任何循环的情况下找到所有具有 z-index 值的 td 吗?

最佳答案

您可以找到所有td,然后过滤掉那些没有设置任何 z-index 的。我相当确定没有办法在比这更具体的级别上进行初始选择。

const test = [
...document.getElementsByTagName("td")
].filter(x => x.style.zIndex !== "")

test.forEach(x => x.style.color = "#"+((1<<24)*Math.random()|0).toString(16))

console.dir(test)
<table>
<thead>
<tr>
<td>name</td>
</tr>
</thead>
<tbody>
<tr>
<td style="z-index: 10">jhon</td>
</tr>
<tr>
<td>
doy
</td>
</tr>
<tr>
<td style="z-index: 20">jam</td>
</tr>
</tbody>
</table>

关于javascript - 如何找到 DOM 中具有 z-index 值的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54310477/

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