我想用features__box
类给块的背景上色,但JS不工作/ Chrome不能识别任何错误。
这是HTML
<div class="features__active features__box">
<h3>Visual Composer</h3>
<p>TheFox comes with the Visual Composer Plugin. You won’t need to code or to remember any shortcodes with our. </p>
</div>
<div class="features__box">
<h3>Responsive</h3>
<p>TheFox comes with the Visual Composer Plugin. You won’t need to code or to remember any shortcodes with our. </p>
</div>
<div class="features__box">
<h3>Retina Ready</h3>
<p>TheFox comes with the Visual Composer Plugin. You won’t need to code or to remember any shortcodes with our. </p>
</div>
THis为JS:
var feature_i = document.querySelectorAll('.features__box');
feature_i.addEventListener('click', function(){
for( var i = 0; i < fearture_i.length; i++) {
feature_i[i].style.backgroundColor = "red";
}
});
默认情况下,每个项目的背景都是白色的。我想让它切换。求求你救命!
发布于 2018-08-29 05:08:19
事件侦听器需要位于您的循环内,并附加到每个对象元素。
var feature_i = document.querySelectorAll('.features__box');
for (var i = 0; i < feature_i.length; i++) {
feature_i[i].addEventListener('click', function() {
this.style.backgroundColor = "red"; // where "this" refers to feature_i[i]
});
}
发布于 2018-08-29 05:05:32
看起来你的for循环不正确。在循环设置的第二部分中,您缺少.length属性。
for( var i = 0; i < fearture_i; i++) {
feature_i[i].style.backgroundColor = "red";
}
https://stackoverflow.com/questions/52066129
复制相似问题