我正在尝试使用香草JS制作个人鉴定模块,当我使用带有收藏夹的“ for”循环时遇到问题-控制台将个人鉴定写为[i] .nextElementSibling为null。手动设置collection元素的索引没有问题-它可以工作。
<section class="testimonials-wrap">
<h2>testimonials</h2>
<div>
<p class="active">Mauris quis nulla ut dui luctus euismod ac sit amet diam. Nulla ullamcorper tortor nec.</p>
<p>Vivamus aliquet massa nec odio sagittis, eget faucibus metus malesuada. Nam sed neque pellentesque, cursus.</p>
<p>Sed bibendum nec ligula sit amet accumsan. Sed neque est, commodo sed tortor in, tincidunt.</p>
</div>
<ul>
<li class="next"><i class="fa fa-chevron-right"></i></li>
<li class="prev"><i class="fa fa-chevron-left"></i></li>
</ul>
</section>
function goToTheNextTest(){
var testimonials = document.getElementsByClassName("testimonials-wrap")[0].getElementsByTagName("div")[0].getElementsByTagName("p");
for(var i = 0; i < testimonials.length; i++){
if(testimonials[i].className === "active"){
testimonials[i].className = "";
//testimonials[i].nextElementSibling.className = "active";
}
}
}
最佳答案
只需以这种方式更改for循环即可。
for(var i = 0; i < testimonials.length-1; i++)
关于javascript - nextElementSibling为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35830104/