addClass和removeClass的用法

一个需求中需要用到样式的改变,自然想到了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;
}

 

公众号:爱唱歌的蜗牛先生

你可能感兴趣的:(踩坑,日常工作)