gpt4 book ai didi

html - 访问未知数量的 div 中的第一个

转载 作者:行者123 更新时间:2023-11-27 23:28:43 25 4
gpt4 key购买 nike

给定以下标记:

<div className="row">
<noscript></noscript>
</div>
<div className="row">
<noscript></noscript>
</div>
<div className="row">
<section></section>
</div>
<div className="row">
<section></section>
</div>
<div className="row">
<section></section>
</div>
...

顶部可能有任意数量的包含noscript的div。

如何只访问中的第一个部分

最佳答案

仅使用 CSS 是不可能实现的,您需要使用 Javascript

解释:

因为 :nth-of-type(1), nth-child(), :first-of-type:first-child 将始终为您提供所有部分,因为它们是第一个 child ,也是其父 div 中类型 section 的第一个。

所有这些选择器只有在您使用 class="row" 将所有部分放在一个父 div 中时才有效。

JavaScript 解决方案:

您可以使用 document.querySelector(".row section") 获取带有 class="row" 的 div 中的第一个部分。

但是使用 CSS 只有这样是不可能的:

document.querySelector(".row section").innerHTML = "I am the first section !";
.row {
width: 200px;
height: 50px;
border: 1px solid black;
}
<div class="row">
<noscript></noscript>
</div>
<div class="row">
<noscript></noscript>
</div>
<div class="row">
<section></section>
</div>
<div class="row">
<section></section>
</div>
<div class="row">
<section></section>
</div>

注意:

同样在 HTML 中它是 class="row" 而不是 className="row",实际上是 classNameJavascript 中使用。

关于html - 访问未知数量的 div 中的第一个 <section>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37045583/

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