gpt4 book ai didi

jquery - 通过循环 div 动态地为 child 添加类

转载 作者:技术小花猫 更新时间:2023-10-29 11:20:15 24 4
gpt4 key购买 nike

我需要一个 jquery 脚本来动态地将类添加到类行中的“div”。该类用于设置边距。如果类行中有两个 div,则仅将类添加到第一个,如果有三个“div”,则将类添加到两个,避免第三个。

所以实际需要是计算“行”上的 div,并向除最后一个之外的 div 添加类。这是我的 html:-

    <div class="row">
<div class="two-col">
<h3>Header 2/3 Column</h3>
<p>Kidney Cancer Canada is a charitable patient-led support organization established to improve the quality of life for patients and their
families living with kidney cancer. <a href="#">Kidney Cancer Canada</a> advocates for access to netreatments, provides support and information to patients,
funds much-needed research, and works to increase awareness of kidney cancer as a significant health issue. Our goal is to help patients navigate
through information about their disease and ensure they have access to new treatment options available to them.</p>
</div>
<div class="one-col">
<h3>Header 1/3 Column</h3>
<p>KCC hosts patient and caregiver education meetings and webcasts from locations all across canada. Atttending meetings in-person provides an
excellent oppurtunity to meet other kidney cancer patients, caregivers, and healthcare professionals</p>
</div>
<div class="clear"></div>
</div>
<div class="row">
<div class="one-col">
<h3>Header 1/3 Column</h3>
<p>KCC hosts patient and caregiver education meetings and webcasts from locations all across canada. Atttending meetings in-person provides an
excellent oppurtunity to meet other kidney cancer patients, caregivers, and healthcare professionals</p>
</div>
<div class="one-col">
<h3>Header 1/3 Column</h3>
<p>KCC hosts patient and caregiver education meetings and webcasts from locations all across canada. Atttending meetings in-person provides an
excellent oppurtunity to meet other kidney cancer patients, caregivers, and healthcare professionals</p>
</div>
<div class="one-col">
<h3>Header 1/3 Column</h3>
<p>KCC hosts patient and caregiver education meetings and webcasts from locations all across canada. Atttending meetings in-person provides an
excellent oppurtunity to meet other kidney cancer patients, caregivers, and healthcare professionals</p>
</div>
<div class="clear"></div>
</div>

最佳答案

如果你不想将该类应用到你的 divclear 那么你可以使用这个

$('div.row').find('div').addClass('yourclass').not(':last-child,.clear');

或者

$('div.row').find('div').addClass('yourclass').not(':last-child');

Live Sample

关于jquery - 通过循环 div 动态地为 child 添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13175290/

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