gpt4 book ai didi

jquery - 为每个 div 增加 z-index

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:58 25 4
gpt4 key购买 nike

我正在设计一个 div 相互重叠的设计,我为类的每个成员分别将 z-index 递增 1。我觉得使用 jquery 有更好的方法。

我想要的是,每个具有类 section 的 div 都有一个 z-index 比前一个大,但不会被 div 丢弃不是特定类的成员,例如

<div class="section" style="z-index:1">Lorem</div>
<div class="section" style="z-index:2">Lorem</div>
<div id="OutOfPlaceDiv">Foo</div>
<div class="section" style="z-index:3">Lorem</div>

最佳答案

使用 each 方法遍历具有该类的元素。

$('.section').each(function(index) {
$(this).css("z-index", index);
});

这会将索引设置为 0、1、2 等。如果您想从 0 以外的地方开始:

var offset = 1;
$('.section').each(function(index) {
$(this).css("z-index", offset + index);
});

关于jquery - 为每个 div 增加 z-index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26519714/

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