gpt4 book ai didi

javascript - 如何定位嵌套在另一个 css 类下的 5 个后代的 css 类

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

我在向下滚动页面时尝试使用 javascript 更改 .box 的不透明度。

var $img = $('.vs-current.box');

我只想定位低于 .vs-current 5 级的 .box。

我不想定位任何 .vs-right 的后代 .box。

为什么 .vs-current.box 而不是针对所有 .box?

谢谢

<section id="section-1" class="vs-current">
<div class="vs-content">
<div class="container">
<div class="intro">
<div class="wrapper-client">
<div class="box" style="opacity: 5.011437074829932;">
<p>Image 1</p>
</div>
<div class="box" style="opacity: 4.07266156462585;">
<p>Image 2</p>
</div>
<div class="box" style="opacity: 3.133886054421769;">
<p>Image 3</p>
</div>
<div class="box" style="opacity: 2.195110544217687;">
<p>Image 4</p>
</div>
<div class="box" style="opacity: 1.2563350340136055;">
<p>Image 5</p>
</div>
<div class="box" style="opacity: 0.3175595238095238;">
<p>Image 6</p>
</div>
<div class="box" style="opacity: -0.6212159863945578;">
<p>Image 7</p>
</div>
</div><!-- END .wrapper-client -->
</div><!-- END .intro -->
</div><!-- END .container -->
</div>
</section>

<section id="section-2" class="vs-right">
<div class="vs-content">
<div class="container">
<div class="intro">
<div class="wrapper-client">
<div class="box" style="opacity: 5.011437074829932;">
<p>Item 1</p>
</div>
<div class="box" style="opacity: 4.07266156462585;">
<p>Item 2</p>
</div>
<div class="box" style="opacity: 3.133886054421769;">
<p>Item 3</p>
</div>
<div class="box" style="opacity: 2.195110544217687;">
<p>Item 4</p>
</div>
<div class="box" style="opacity: 1.2563350340136055;">
<p>Item 5</p>
</div>
<div class="box" style="opacity: 0.3175595238095238;">
<p>Item 6</p>
</div>
<div class="box" style="opacity: -0.6212159863945578;">
<p>Item 7</p>
</div>
</div><!-- END .wrapper-client -->
</div><!-- END .intro -->
</div><!-- END .container -->
</div>
</section>

最佳答案

对于你想使用的后代

var $img = $('.vs-current .box');
$("#postResult").html($img.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<b>object length: <div id="postResult"></div></b>
<section id="section-1" class="vs-current">
<div class="vs-content">
<div class="container">
<div class="intro">
<div class="wrapper-client">
<div class="box" style="opacity: 5.011437074829932;">
<p>Image 1</p>
</div>
<div class="box" style="opacity: 4.07266156462585;">
<p>Image 2</p>
</div>
<div class="box" style="opacity: 3.133886054421769;">
<p>Image 3</p>
</div>
<div class="box" style="opacity: 2.195110544217687;">
<p>Image 4</p>
</div>
<div class="box" style="opacity: 1.2563350340136055;">
<p>Image 5</p>
</div>
<div class="box" style="opacity: 0.3175595238095238;">
<p>Image 6</p>
</div>
<div class="box" style="opacity: -0.6212159863945578;">
<p>Image 7</p>
</div>
</div><!-- END .wrapper-client -->
</div><!-- END .intro -->
</div><!-- END .container -->
</div>
</section>

<section id="section-2" class="vs-right">
<div class="vs-content">
<div class="container">
<div class="intro">
<div class="wrapper-client">
<div class="box" style="opacity: 5.011437074829932;">
<p>Item 1</p>
</div>
<div class="box" style="opacity: 4.07266156462585;">
<p>Item 2</p>
</div>
<div class="box" style="opacity: 3.133886054421769;">
<p>Item 3</p>
</div>
<div class="box" style="opacity: 2.195110544217687;">
<p>Item 4</p>
</div>
<div class="box" style="opacity: 1.2563350340136055;">
<p>Item 5</p>
</div>
<div class="box" style="opacity: 0.3175595238095238;">
<p>Item 6</p>
</div>
<div class="box" style="opacity: -0.6212159863945578;">
<p>Item 7</p>
</div>
</div><!-- END .wrapper-client -->
</div><!-- END .intro -->
</div><!-- END .container -->
</div>
</section>

注意额外的空间。查看结果是 7,而不是 14

关于javascript - 如何定位嵌套在另一个 css 类下的 5 个后代的 css 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28339628/

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