简体   繁体   English

如何从JS更改动态CSS类的bg颜色

[英]How to change bg color of dynamic CSS class from JS

I have a bar chart and I want to be able to assign individual fill colors to bars depending on a condition. 我有一个条形图,我希望能够根据条件为条形分配单独的填充颜色。 I don't think it matters, but I am using angular-nvd3 linePlusBarChart to draw bars. 我认为这并不重要,但是我正在使用angular-nvd3 linePlusBarChart绘制条。

UPDATE There seemingly is an issue with my local environment, possibly with NVD3 or angular libraries, I am not sure what's going on yet. 更新我的本地环境似乎存在问题,可能是NVD3或角度库,我不确定发生了什么。 I put together jsfiddle and everything worked perfectly. 我把jsfiddle放在一起,一切工作正常。 See the link below, the bars can have different colors. 请参见下面的链接,这些条可以具有不同的颜色。 On my local computer though, when the execution reaches the point var y = document.querySelector(...., I am getting angular error in console ->>>>> y is null. 但是,在我的本地计算机上,当执行到达该点时,var y = document.querySelector(....,控制台中出现角度错误->>>>> y为空。

http://jsfiddle.net/p0g9Lqu8/ http://jsfiddle.net/p0g9Lqu8/

Here is the HTML. 这是HTML。

<nvd3>
<g class="nv-bars">
    <rect x="0" y="419" height="1"  fill="LimeGreen" class="nv-bar positive nv-bar-0-0" width="267"></rect>
    <rect x="0" y="252" height="168"  fill="LimeGreen" class="nv-bar positive nv-bar-0-1" width="267"></rect>
    <rect x="0" y="294" height="126"  fill="LimeGreen" class="nv-bar positive nv-bar-0-2 hover" width="267"></rect>
    <rect x="0" y="252" height="168"  fill="LimeGreen" class="nv-bar positive nv-bar-0-3" width="267"></rect>
</g>
</nvd3>

As you can see, each rect tag has a dynamic CSS class nv-bar-0-1, nv-bar-0-2.. that I would like to be able to point to from JS. 如您所见,每个rect标记都有一个动态CSS类nv-bar-0-1,nv-bar-0-2 ..我希望能够从JS指向它。 NOTE: from HTML, if I use CSS, all works, but from JS loop, does not. 注意:从HTML,如果我使用CSS,则所有工作正常,但从JS循环开始,一切正常。 Any help is appreciated. 任何帮助表示赞赏。

This perfectly works. 这完美地工作。

<style>
nvd3 .nv-bars rect {
    fill:white;
}

nvd3 .nv-bars rect.nv-bar-0-1{
    fill:yellow;
}
  </style>

But this, does not: 但这并不能:

for(var i = 0; i < 4; i++) {
     var y = document.querySelector('nvd3 .nv-bars rect.nv-bar-0-' + i); 
     y.style.fill = "#ffff00";
}

OR this one, same result 或与此相同的结果

var y = document.querySelector('nvd3 .nv-bars rect.nv-bar-0-1); // y is always null
y.style.fill = "#ffff00"; // triggers null reference error here

I call that JS fragment inside Angular controller in this block: 我在此块中的Angular控制器中调用该JS片段:

     angular.element(document).ready(function () {
        var y = document.querySelector('nvd3 .nv-bars rect.nv-bar-0-1');
        console.log('>>>>>>>>>>>===' + y); // this outputs [object    HTMLUnknownElement]
        y.style.fill = "#ffff00";
    });

I thank everybody for time and input. 我感谢大家的时间和投入。 I finally found where the problem was and what the solution would be, so I would like to share it with the community in case anybody comes across a similar problem. 我终于找到了问题所在和解决方案,所以我希望与社区分享该问题,以防有人遇到类似问题。

My final output into the browser is a result of combining multiple HTML/JS/CSS fragments. 我最终输入浏览器的结果是结合了多个HTML / JS / CSS片段的结果。 The issue was in timing: the JS code was attempting to refer to a tag, but it appeared the dynamic construction of that tag with its HTML/CSS was not complete at the time of JS reference. 问题在于时机:JS代码试图引用一个标签,但是在JS引用时,带有HTML / CSS的标签动态构建似乎还没有完成。

angular.element(document).ready(.... could not do the job, but a pure JS approach from the link below - did. angular.element(document).ready(....无法完成这项工作,但是来自下面链接的纯JS方法-可以。

https://github.com/jfriend00/docReady https://github.com/jfriend00/docReady

I enclosed the JS block that was to check some conditions and assign colors to bars, into the docReady block. 我将用于检查某些条件并将颜色分配给条的JS块封装到docReady块中。 See the simplified code sample below. 请参见下面的简化代码示例。

           docReady(function () {

                for (var i = 0; i < 4; i++) {
                    var k = document.querySelector('nvd3 .nv-bars rect.nv-bar-0-' + i);
                    // some condition goes here.....
                    k.style.fill = "green";

                }

                var y = document.querySelector('nvd3 .nv-bars rect.nv-bar-0-1');
                // this too changes color
                y.style.fill = "#ff0000";
         });

Worked in all major browsers: Firefox, Chrome, Safari and IE 适用于所有主要浏览器:Firefox,Chrome,Safari和IE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM