2 回答

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊
不確定“僅使用純 JS”classList.toggle()是什么意思,但對(duì)此非常有用。我還建議您將樣式保留在 CSS 中,而不是 JS 或 HTML。
const buttonElement = document.querySelector('.js-toggle-button')
const paragraphElement = document.querySelector('.js-paragraph')
buttonElement.addEventListener('click', () => {
paragraphElement.classList.toggle('paragraph--off')
})
.paragraph {
color: red;
}
/* Overrides because of specificity */
.paragraph--off {
color: blue;
}
<p class="js-paragraph paragraph">Paragraph</p>
<button class="js-toggle-button">Toggle</button>

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
我修改了你的 jsfidle檢查這個(gè)
let firstCall = true;
function mudarred(){
if(firstCall){
document.getElementById('red').style.color="red";
firstCall= false;
}else{
firstCall=true;
document.getElementById('red').style.color="blue";
}
}
添加回答
舉報(bào)