如何使用 jquery 将折叠 class 动态添加到相同 class 的多个 div
how to dynamically Add collapse class to multiple divs of same class using jquery
我有一个包含多个相同 div 的页面 class,我想为所有 div 动态添加折叠 classes。
我的 html :
<div class="section-title"></div>
<div class="section-text"></div>
<div class="section-title"></div>
<div class="section-text"></div>
<div class="section-title"></div>
<div class="section-text"></div>
我的 jquery :
var numItems = $('.section-title').length;
var i;
for(i='0';i<numItems;i++) {
$(".section-title ").attr("data-toggle", "collapse");
$(".section-title ").attr("data-target", "#collapseOne"+i);
$(".section-text ").attr("data-toggle", "collapse");
$(".section-text").attr("id", "collapseOne"+i);
}
但这不起作用,因为我想为 div 附加 collpaseOne1、collpaseOne2、collpaseOne3 classes,但它为所有 div 附加 collpaseOne3。
那是因为你每次都针对一个,需要单独执行操作
$(".section-title").each(function(i, element){
$(this).attr({
"data-toggle": "collapse",
"data-target": "#collapseOne"+i
})
});
$(".section-text").each(function(i, element){
$(this).attr({
"data-toggle": "collapse",
"id": "collapseOne"+i
})
});
像这样
$(function(){
const $sections = $('.section-title')
for (let i = 0; i< $sections.length; i++){
const $section = $sections.eq(i)
const $sectionText = $section.next()
$section.text(`Section ${i}`)
$sectionText.text(`Section Text ${i}`)
const targetId = `section_${i}`
$sectionText.attr('id',targetId)
$sectionText.addClass('collapse')
$section.attr('data-toggle','collapse')
$section.attr('data-target',`#${targetId}`)
}
})
.section-title{
font-weight:bold;
cursor:pointer;
padding:.5em;
}
.section-text{
padding:.5em;
margin-bottom:1em;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="section-title"></div>
<div class="section-text"></div>
<div class="section-title"></div>
<div class="section-text"></div>
<div class="section-title"></div>
<div class="section-text"></div>
我有一个包含多个相同 div 的页面 class,我想为所有 div 动态添加折叠 classes。
我的 html :
<div class="section-title"></div>
<div class="section-text"></div>
<div class="section-title"></div>
<div class="section-text"></div>
<div class="section-title"></div>
<div class="section-text"></div>
我的 jquery :
var numItems = $('.section-title').length;
var i;
for(i='0';i<numItems;i++) {
$(".section-title ").attr("data-toggle", "collapse");
$(".section-title ").attr("data-target", "#collapseOne"+i);
$(".section-text ").attr("data-toggle", "collapse");
$(".section-text").attr("id", "collapseOne"+i);
}
但这不起作用,因为我想为 div 附加 collpaseOne1、collpaseOne2、collpaseOne3 classes,但它为所有 div 附加 collpaseOne3。
那是因为你每次都针对一个,需要单独执行操作
$(".section-title").each(function(i, element){
$(this).attr({
"data-toggle": "collapse",
"data-target": "#collapseOne"+i
})
});
$(".section-text").each(function(i, element){
$(this).attr({
"data-toggle": "collapse",
"id": "collapseOne"+i
})
});
像这样
$(function(){
const $sections = $('.section-title')
for (let i = 0; i< $sections.length; i++){
const $section = $sections.eq(i)
const $sectionText = $section.next()
$section.text(`Section ${i}`)
$sectionText.text(`Section Text ${i}`)
const targetId = `section_${i}`
$sectionText.attr('id',targetId)
$sectionText.addClass('collapse')
$section.attr('data-toggle','collapse')
$section.attr('data-target',`#${targetId}`)
}
})
.section-title{
font-weight:bold;
cursor:pointer;
padding:.5em;
}
.section-text{
padding:.5em;
margin-bottom:1em;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="section-title"></div>
<div class="section-text"></div>
<div class="section-title"></div>
<div class="section-text"></div>
<div class="section-title"></div>
<div class="section-text"></div>