使用javascript为每个类别设置不同的颜色

发布于 2025-01-11 05:25:16 字数 2172 浏览 0 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2025-01-18 05:25:16

您可以使用对象作为字典将“slug”翻译为“color”。

您可以在categories.map内部函数中设置颜色属性。

我删除了 id="cat" 因为这会导致重复的 ID,这永远不会好。

const colorMap = {
  "animation": animColor,
  "decor": decorColor,
  "illustrations": illuColor,
  "developpement-visuel": devVisuel,
  "realisations": real,
  "croquis": 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 className="uppercase font-light text-sm" style=`color: ${colorMap[category.attributes.slug]}`>{category.attributes.name}</a>
                                    </Link>
                                </li>
                            )
                        })}
                    </ul>
                </nav>
            </div>
        </div>

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.

const colorMap = {
  "animation": animColor,
  "decor": decorColor,
  "illustrations": illuColor,
  "developpement-visuel": devVisuel,
  "realisations": real,
  "croquis": 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 className="uppercase font-light text-sm" style=`color: ${colorMap[category.attributes.slug]}`>{category.attributes.name}</a>
                                    </Link>
                                </li>
                            )
                        })}
                    </ul>
                </nav>
            </div>
        </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文