我很困惑为什么这个javascript不改变我的h2元素的颜色

I am baffled why this javascript is not changing the color of my h2 elements

本文关键字:我的 改变 h2 元素 颜色 javascript 为什么      更新时间:2023-09-26

我试图根据之前的选择(男性或女性)为所有strong和h2标签上色。我已经对以前的li和a标签这样做了,所以我很困惑为什么这不起作用。我一定得去买只橡皮鸭,因为我已经被困了一段时间,这似乎太容易了。

CSS:

.colorPink {
    color: #a0426a!important;
}
.colorBlue {
    color: blue!important;
}

Javascript(传入男性/女性字符串,这段代码确实被击中b/c其他东西被着色):

function SetGender(e)
{
    sessionStorage.gender = e;
    if (sessionStorage.gender == "male") {
        document.getElementById("category-list").className += " colorBlue";
        //..many other class/id color changes
        //Change H2/Strong tag color color
        $("#adviceBodyDisplay h2").addClass("colorBlue");
    }
    else if(sessionStorage.gender=="female") {
        document.getElementById("category-list").className += " colorPink";
        //..many other class/id color changes
        //Change H2/Strong tag color color
        $("#adviceBodyDisplay h2").addClass("colorPink");
    }
}

HTMLstuff:

<div id="adviceBodyDisplay">
  <h2>Real Life Example 1:</h2>
  <p>other text</p>
</div>

没有ID为"category-list"的元素,所以你的Javascript在到达颜色更改前就崩溃了