【发布时间】:2021-11-25 18:46:59
【问题描述】:
我有一个 json 文件,我在其中存储我的数据以生成某种图表。我希望能够根据 json 中的属性有条件地更改图表的颜色。为假时颜色应为红色,为真时应为绿色。 这是我的 json 文件:
"children": [
{
"children": [
{
"children": [],
"id": 50,
"name": "gfj",
"checked": true
}
],
"id": 51,
"name": "malek"
},
{
"children": [
{
"children": [
{
"children": [],
"id": 49,
"name": "nice",
"checked": true
}
],
"id": 48,
"name": "amira",
"checked": false
}
],
"id": 47,
"name": "mahdi"
}
],
我能够从 json 文件中获取属性“已检查”到我的 js 文件中,但我不知道如何更改颜色,因为颜色在 css 文件中已更改。
_checked = function(d){ return d.checked; },
这是css文件:
/*
Example fishbone styling... note that you can't actually change
line markers here, which is annoying
*/
html, body{ margin: 0; padding: 0; overflow: hidden;}
/* get it? gill? */
*{ font-family: "serif", "serif"; }
.label-0{ font-size: 2em }
.label-1{ font-size: 1.5em; fill: #06405a; }
.label-2{ font-size: 1em; fill: #06405a; }
.label-3{ font-size: .9em; fill: #888; }
.label-4{ font-size: .8em; fill: #aaa; }
.link-0{ stroke: #188fc6; stroke-width: 3px}
.link-1{ stroke: #188fc6; stroke-width: 2px}
.link-2, .link-3, .link-4{ stroke: #188fc6; stroke-width: 2px; }
链接是图中的箭头,每个箭头都有自己的颜色和大小。我的问题是:如何使用我在 js 文件中获取的属性“已检查”并在 css 文件中动态更改颜色。非常感谢。
【问题讨论】:
-
你不能改变 CSS 文件的内容,你也不需要。但是您可以有条件地将特定的 CSS 样式(颜色)添加到您的 HTML 中。这包括更改 HTML 元素上的属性,例如
class或style。
标签: javascript html css json angular