How can I set data-slide-to, same as I set id for each li#myCarousel
HTML
I have set ids 0, 1 ...
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="" id="0"></li>
<li data-target="#myCarousel" data-slide-to="" id="1"></li>
</ol>
JS
var iD = document.querySelectorAll('.carousel-indicators li');
// Set their ID
for (var i = 0; i < iD.length; i++)
iD[i].id = + i;
I need set also data-slide-to 0, 1 ...
JS
var dataslideto = document.querySelectorAll('.carousel-indicators li');
// Set their data-slide-to
for (var i = 0; i < dataslideto.length; i++)
dataslideto[i].data-slide-to = + i;
HTML OUTPUT I NEED
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" id="0"></li>
<li data-target="#myCarousel" data-slide-to="1" id="1"></li>
</ol>
* JOB done i have replace js to *
var i = 0;
$(".carousel-indicators li").each( function() {
$(this).attr("data-slide-to", +i);
$(this).attr("id", +i);
i++;
});
You set the ID based on the index of the for
loop, at the same time you can also set the data attribute:
var abcElements = document.querySelectorAll('.carousel-indicators li');
for (var i = 0; i < abcElements.length; i++) {
abcElements[i].id = + i;
abcElements[i].setAttribute('data-slide-to', i);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.