gpt4 book ai didi

css - 原型(prototype) : How do I find the first element with a certain class?

转载 作者:行者123 更新时间:2023-11-28 12:21:37 25 4
gpt4 key购买 nike

我的文档中有 2 个 ul 元素。如何使用 PrototypeJS 找到 2 个中的第一个?我试过这段代码:

first = $$('ul[class="level0"]')[0];
second = $$('ul[class="level0"]')[1];

只有第一个是填充的,第二个是空的。有任何想法吗?这是我的 html:

<ul id="nav">
<li class="level0 nav-1 first level-top parent">
<ul class="level0">...</ul>
</li>
<li class="level0 nav-2 last level-top parent">
<ul class="level0">...</ul>
</li>
</ul>

谢谢:)

最佳答案

不要使用属性 CSS 选择器,而是使用类 CSS 选择器

first = $$('ul.level0')[0];
second = $$('ul.level0')[1];

但除此之外应该可行

还有其他方法 $$() 返回一个元素数组(即使是一个),你可以引用 .first()

first = $$('ul.level0').first()

请让我们知道修复 HTML 是否有效,或者如果您在 javascript 控制台中遇到任何错误 - 这可能会导致您遇到不同的问题

关于css - 原型(prototype) : How do I find the first element with a certain class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648350/

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