[英]Add class to child elements of div's with same class using jQuery
I am trying to add a class to the first "img" element of every parent but I cannot manage to do it after several tries.我正在尝试将 class 添加到每个父母的第一个“img”元素中,但经过几次尝试后我无法做到。 I am trying to add an active class to the first img of every carousel but does not seem to be working.
我正在尝试将活动的 class 添加到每个轮播的第一个 img 中,但似乎不起作用。
Below is the jsfiddle link basically I want the first images to show but my jQuery does not work.下面是 jsfiddle 链接,基本上我希望显示第一张图片,但我的 jQuery 不起作用。
https://jsfiddle.net/60mq41nw/ https://jsfiddle.net/60mq41nw/
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<!-- second carousel -->
<div id="carouselExampleControls2" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="First slide2">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Second slide2">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Third slide2">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls2" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls2" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
js code: js代码:
$(document).ready(function () {
$('.carousel').each(function() {
var c = this;
$(c).find("img").first().addClass("active");
});
});
Thanks in advance!提前致谢!
Your code works just fine (It's already append the active class to the first img
).. So i think you want to add the active class to carousel-item
because the bootstrap carousel append the active class to the carousel-item
not to the img
.. try the next code Your code works just fine (It's already append the active class to the first
img
).. So i think you want to add the active class to carousel-item
because the bootstrap carousel append the active class to the carousel-item
not to the img
.. 尝试下一个代码
$(document).ready(function () {
$('.carousel').each(function() {
var c = this;
$(c).find(".carousel-item").removeClass('active').filter(':first').addClass("active");
});
});
And To be honest I believe there is a built-in method in bootstrap carousel itself to let you achieve this.. I don't know the method because unfortunately I'm not expert in bootstrap老实说,我相信引导轮播本身有一个内置方法可以让您实现这一目标。我不知道该方法,因为不幸的是我不是引导专家
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.