How to toggle class using jquery if else statement?

前端 未结 5 1475
渐次进展
渐次进展 2021-01-20 17:14

I want to toggle class using jquery If Else statement for specified conditions.

My Html with default css class=\"horizontal\"

5条回答
  •  时光说笑
    2021-01-20 17:39

    In case you do not want to use the toogleClass(), here is another solution:

    http://jsfiddle.net/YFBWQ/

    $('#foo-bar')
    .removeClass('horizontal vertical')
    .addClass( myvar=='layout2' ? 'vertical' : 'horizontal')
    

    or

    $('#foo-bar')
    .removeClass(myvar=='layout2' ? 'horizontal' : 'vertical')
    .addClass(myvar=='layout2' ? 'vertical' : 'horizontal')
    

提交回复
热议问题