首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >js样式不起作用

js样式不起作用
EN

Stack Overflow用户
提问于 2018-08-29 04:59:54
回答 2查看 45关注 0票数 0

我想用features__box类给块的背景上色,但JS不工作/ Chrome不能识别任何错误。

这是HTML

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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";
    }

});

默认情况下,每个项目的背景都是白色的。我想让它切换。求求你救命!

EN

回答 2

Stack Overflow用户

发布于 2018-08-29 05:08:19

事件侦听器需要位于您的循环内,并附加到每个对象元素。

代码语言:javascript
复制
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]
  });
}
票数 2
EN

Stack Overflow用户

发布于 2018-08-29 05:05:32

看起来你的for循环不正确。在循环设置的第二部分中,您缺少.length属性。

代码语言:javascript
复制
for( var i = 0; i < fearture_i; i++) {
    feature_i[i].style.backgroundColor = "red";
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52066129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档