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

时间:2014-07-31 18:56:08

标签: javascript jquery html css

我试图根据之前的选择(男性或女性)为所有强力和h2标签着色。我已经对之前的li和标签做了这个,所以我很困惑为什么这不起作用。我必须去购买一个橡皮鸭子b / c我已经卡住了一段时间,这似乎太容易了。

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>

1 个答案:

答案 0 :(得分:0)

没有ID&#34; category-list&#34;的元素,所以你的Javascript在变色之前会崩溃