[英]JQuery add class dynamically to first element with a particular class
I have dynamically created bootstrap accordions containing dynamically created groups in each. 我已经动态创建了Bootstrap 手风琴 ,每个中都包含动态创建的组。 I want to add the
in
class to the first group in each accordion, which opens it as you can see in the linked bootstrap example. 我想将
in
类添加到每个手风琴的第一个组中,如您在链接的引导示例中看到的那样,它将打开它。
<div class="panel panel-default">
<div class="panel-heading">
<h6 class="panel-title">
<a class="accordionToggle" data-toggle="collapse" data-parent="#dynamicId" href="#dynamicId">
Title
</a>
</h6>
</div>
<div class="panel-collapse collapse" id="dynamicId">
...
</div>
</div>
...
There can be any number of .panel.panel-default
which can contain any number of .panel-collapse.collapse
可以有任意数量的
.panel.panel-default
,可以包含任意数量的.panel-collapse.collapse
Here is what I have tried, as well as some other variations, to add the in
class to the first .panel-collapse.collapse
of each .panel.panel-default
but I keep ending up with it adding the class to each one and not just the first. 这是我尝试过的方法以及其他一些变体,将
in
类添加到每个.panel.panel-default
的第一个.panel-collapse.collapse
,但是我一直坚持到最后,将其添加到每个类中不只是第一个。
$(".panel.panel-default").each(function (index) {
$(this).children(".panel-collapse.collapse:first").addClass("in");
});
$(".panel.panel-default").each(function (index) {
$(this).find(".panel-collapse.collapse:first").addClass("in");
});
$(".panel.panel-default").each(function (index) {
$(this).children(".panel-collapse.collapse").first().addClass("in");
});
Any help will be appreciated! 任何帮助将不胜感激!
You need to iterate the panel-group
, because I think what you are trying to do is to expand the first item in each accordion 您需要迭代
panel-group
,因为我认为您要尝试的是扩展每个手风琴中的第一项
$(".panel-group").each(function (index) {
$(this).children(".panel-collapse.collapse:first").addClass("in");
});
Simply add this: 只需添加以下内容:
$(document).ready(function(){
$(".panel-collapse.collapse:first").addClass("in");
});
It works for me. 这个对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.