使用javascript为每个类别设置不同的颜色
我试图在 javascript 中实现一些目标,
我想为每个类别设置特定的颜色
例如: 如果category.name == x则颜色=蓝色 如果category.name == y则颜色=红色 ...
我尝试过这样,但我想我错过了一些东西:
categories.forEach(category => {
if (category.attributes.slug = "animation") {
cat.style.color = animColor;
}
if (category.attributes.slug = "decor") {
cat.style.color = decorColor;
}
if (category.attributes.slug = "illustrations") {
cat.style.color = illuColor;
}
if (category.attributes.slug = "developpement-visuel") {
cat.style.color = devVisuel;
}
if (category.attributes.slug = "realisations") {
cat.style.color = real;
}
if (category.attributes.slug = "croquis") {
cat.style.color = croquis;
}
});
<div>
<Header
categories={categories}
homepage={homepage} />
<div className="hidden md:block px-5 pt-10">
<a className="hover:no-underline " href="/">
<h3 className="text-xl uppercase tracking-widest mb-4">{homepage.attributes.hero.title}</h3>
</a>
<h5 className="text-sm mb-10">{homepage.attributes.bio.text}</h5>
<nav>
<ul>
{categories.map((category) => {
return (
<li key={category.id} className="mb-4">
<Link href={`/category/${category.attributes.slug}`}>
<a id="cat" className="uppercase font-light text-sm">{category.attributes.name}</a>
</Link>
</li>
)
})}
</ul>
</nav>
</div>
</div>
你能帮我找到解决方案吗?
I'm trying to achieve something in javascript
I want to set a specific color to each categories
For exemple :
if catergory.name == x then color = blue
if category.name == y then color = red
...
I tried like this, but I think I miss something :
categories.forEach(category => {
if (category.attributes.slug = "animation") {
cat.style.color = animColor;
}
if (category.attributes.slug = "decor") {
cat.style.color = decorColor;
}
if (category.attributes.slug = "illustrations") {
cat.style.color = illuColor;
}
if (category.attributes.slug = "developpement-visuel") {
cat.style.color = devVisuel;
}
if (category.attributes.slug = "realisations") {
cat.style.color = real;
}
if (category.attributes.slug = "croquis") {
cat.style.color = croquis;
}
});
<div>
<Header
categories={categories}
homepage={homepage} />
<div className="hidden md:block px-5 pt-10">
<a className="hover:no-underline " href="/">
<h3 className="text-xl uppercase tracking-widest mb-4">{homepage.attributes.hero.title}</h3>
</a>
<h5 className="text-sm mb-10">{homepage.attributes.bio.text}</h5>
<nav>
<ul>
{categories.map((category) => {
return (
<li key={category.id} className="mb-4">
<Link href={`/category/${category.attributes.slug}`}>
<a id="cat" className="uppercase font-light text-sm">{category.attributes.name}</a>
</Link>
</li>
)
})}
</ul>
</nav>
</div>
</div>
Can you help me find the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用对象作为字典将“slug”翻译为“color”。
您可以在categories.map内部函数中设置颜色属性。
我删除了 id="cat" 因为这会导致重复的 ID,这永远不会好。
You can use an object as a dictionary translating "slug" to "color".
You can set the color attribute in the categories.map inner function.
I removed id="cat" because that would result in duplicated IDs which are never good.