我想每次單擊按鈕時添加課程並更改段落中的文本。我怎樣才能做到這一點?我對JavaScript很陌生,所以任何幫助將不勝感激!添加課程並更改HTML
HTML
<h1 id="heading">Hello!</h1>
<button onClick = "good()">Click Me</button>
CSS
.pink{
color:pink;
}
.blue{
color:blue;
}
.red {
color:red;
}
JS
function good(){
var computerChoice = Math.random();
var heading = document.getElementById('heading');
if(computerChoice <= 0.33){
heading.innerHTML = "This is a good!";
heading.addClass(pink);
}
if(computerChoice >= 0.67){
heading.innerHTML = "This is a bad";
heading.addClass(blue);
}
else {
heading.innerHTML = "This is else";
} heading.addClass(red);
}
對我們而言有何反饋? –