addClass和removeClass的用法
阿新 • • 發佈:2019-01-07
一個需求中需要用到樣式的改變,自然想到了addClass和removeClass,但是在用的犯了一個很低階的錯誤,導致糾結了半天。
初始寫法(錯誤寫法):
if (isPlayMusic) { console.log('music-to-paused'); $('.fortune-test-activity-head-music').addClass('.fortune-test-activity-head-music-paused'); isPlayMusic = false; } else { console.log('music-to-running'); $('.fortune-test-activity-head-music').removeClass('.fortune-test-activity-head-music-paused'); isPlayMusic = true; }
後來發現原來在addClass和removeClass的class前面不能加'.'。
更改寫法(正確寫法):
if (isPlayMusic) { console.log('music-to-paused'); $('.fortune-test-activity-head-music').addClass('fortune-test-activity-head-music-paused'); isPlayMusic = false; } else { console.log('music-to-running'); $('.fortune-test-activity-head-music').removeClass('fortune-test-activity-head-music-paused'); isPlayMusic = true; }