removeClass() if it exists

后端 未结 6 949
时光说笑
时光说笑 2020-12-29 20:01

This function adds a rotated class to my button when I click it. The button has an arrow on it which points in the direction the panel has slid.

How co

相关标签:
6条回答
  • 2020-12-29 20:42

    You can use .toggleClass()

    $('#btnDiv').toggleClass('rotated');
    

    That adds it if it's missing, and removes it if it's present. There's also .is() to check for things like that:

    if ($('#btnDiv').is('.rotated'))
    

    or more simply:

    if ($('#btnDiv').hasClass('rotated'))
    
    0 讨论(0)
  • Just use .toggleClass() to acheive that.

    0 讨论(0)
  • 2020-12-29 20:44
    $("#btnDiv").click(function (){
        $('#slidePanel').toggle( "slide",{direction: 'right'});
        if($('#btnDiv').hasClass('rotated')){
              $('#btnDiv').removeClass('rotated');
        }
        else{
             $('#btnDiv').addClass('rotated');
        }
      });
    
    0 讨论(0)
  • 2020-12-29 20:44

    you can you simple swith the class on click

    
    $('.myclassname').on('click', function(){
        $('.myclass h2').toggleClass( 'first_class', 'second_class');
    }); 
    
    0 讨论(0)
  • Try this

    if($('#btnDiv').hasClass('rotated')){
       $('#btnDiv').removeClass('rotated')
    }else{
      $('#btnDiv').addClass('rotated')
    }
    
    0 讨论(0)
  • 2020-12-29 20:57
    if($('#btnDiv').hasClass('rotated')){
       $('#btnDiv').removeClass('rotated')
    }else{
       $('#btnDiv').addClass('rotated')
    }
    
    0 讨论(0)
提交回复
热议问题