简体   繁体   English

如何使用 jquery 计算父 div 中的子 div 并显示每个子 div 中的订单号?

[英]How can I use jquery to count the child divs within a parent div and display the order number within each child div?

I'm currently building a CMS gallery list within Webflow and I'd like to automatically display a number within each individual child div.我目前正在 Webflow 中构建一个 CMS 画廊列表,我想在每个单独的子 div 中自动显示一个数字。

I can see that I probably need to implement some sort of jquery code and I'm struggling rewrite existing solutions.我可以看到我可能需要实现某种 jquery 代码,并且我正在努力重写现有的解决方案。

In the code below, I want to count the number of children within.feature-slider_list and then display the child order number within.feature-slider_number.在下面的代码中,我想计算 .feature-slider_list 中的子项数量,然后显示 .feature-slider_number 中的子项订单号。

Any help would be much appreciated!任何帮助将非常感激!

 .feature-slider_item { width: 200px; height: 200px; margin-bottom: 50px; background-color: black; }.feature-slider_wrap { position: relative; }.feature-slider_number { position: absolute; left: 30px; top: auto; right: auto; bottom: 30px; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 70px; height: 70px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; border-radius: 10px; background-color: #00d6d6; -webkit-transform: rotate(-6deg); -ms-transform: rotate(-6deg); transform: rotate(-6deg); color: #fff; font-size: 2rem; line-height: 2.5rem; font-weight: 700; }
 <div class="feature-slider_list"> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number">1</div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number">1</div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number">1</div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number">1</div> </div> </div> </div>

Please add jQuery code to loop through the slider items and add index.请添加 jQuery 代码以循环遍历 slider 项并添加索引。

 $(document).ready(function(){ $('.feature-slider_list').children().each(function (index) { $(this).find('.feature-slider_number').html(index+1); }); } )
 .feature-slider_item { width: 200px; height: 200px; margin-bottom: 50px; background-color: black; }.feature-slider_wrap { position: relative; }.feature-slider_number { position: absolute; left: 30px; top: auto; right: auto; bottom: 30px; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 70px; height: 70px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; border-radius: 10px; background-color: #00d6d6; -webkit-transform: rotate(-6deg); -ms-transform: rotate(-6deg); transform: rotate(-6deg); color: #fff; font-size: 2rem; line-height: 2.5rem; font-weight: 700; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="feature-slider_list"> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number">1</div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number">1</div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number">1</div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number">1</div> </div> </div> </div>

Use ::after and content with attr .使用::aftercontent attr And I added height:100% to feature-slider_item and bottom: -30px;我将height:100%添加到feature-slider_itembottom: -30px; to .feature-slider_number to fix that one element with an index was not showing up..feature-slider_number以修复未显示具有索引的元素。

 .feature-slider_item { width: 200px; height: 200px; margin-bottom: 50px; background-color: black; }.feature-slider_wrap { position: relative; height:100%; }.feature-slider_number::after { content: attr(index); }.feature-slider_number { position: absolute; left: 30px; top: auto; right: auto; bottom: -30px; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 70px; height: 70px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; border-radius: 10px; background-color: #00d6d6; -webkit-transform: rotate(-6deg); -ms-transform: rotate(-6deg); transform: rotate(-6deg); color: #fff; font-size: 2rem; line-height: 2.5rem; font-weight: 700; }
 <div class="feature-slider_list"> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number" index="1"></div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number" index="2"></div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number" index="3"></div> </div> </div> <div class="feature-slider_item"> <div class="feature-slider_wrap"> <img src="" class="feature-slider_image"> <div class="feature-slider_number" index="4"></div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM