我很困惑我在做什麼錯在這裏我跟着一個教程,它適用於他們,但不是我。我已經切換了很多東西來試圖找到問題,但它似乎並不奏效。如何用JavaScript中的按鈕單擊來更改css類?
HTML
<h1 class="settingOne" id="mainText">Simple HTML Page</h1>
<p class="center">
This is a very simple HTML page.
</p>
<p class="center">It's about as basic as they come. It has: </p>
<ul class="center">
<li>An H1 Tag</li>
<li>Two paragraphs</li>
<li>An unordered list</li>
</ul>
<button id="changeButton">
<p>
Hey push this to change the header thing
</p>
</button>
CSS
.settingOne {
font-family: fantasy;
color: blue;
text-align: center;
}
.settingTwo {
font-family: serif;
color: red;
text-align: center;
}
JAVASCRIPT
function changeClass() {
document.getElementById("changeButton").onclick = function() {
if (document.getElementById("mainText").className === "settingOne") {
document.getElementById("mainText").className = "settingTwo";
} else {
document.getElementById("mainText").className === "settingOne";
}
};
}
window.onload = function() {
preparePage();
}
;
你是不是要求您onload處理中的函數調用 –
changeClass()。 (什麼是從onload調用但未顯示的preparePage()。) – nnnnnn
'document.getElementById(「mainText」)。className ===「settingOne」;' 您應該爲此行分配值,其他聲明,而不是檢查 –