如何使用jquery if else语句切换类? [英] How to toggle class using jquery if else statement?

查看:90
本文介绍了如何使用jquery if else语句切换类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jquery If Else语句在指定条件下切换类.

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

使用默认CSS class="horizontal"

<div id="foo-bar"  class="my-item horizontal"> 
</div>

现在我该如何使用jquery(其中以var=mayvar作为条件)在horizontalvertical之间打乱我的CSS类.

Now How can I shuffle my css class between horizontal and vertical using jquery where var=mayvar as condition.

myvar may be layout1 or layout2.

对于myvar=layout1 class="horizontal" 对于myvar="layout2 class="vertical"

如果在更多不同的CSS类中存在两个以上条件,该如何编写jquery?

and how to write the jquery if there are more than two condition for more different css classes?

推荐答案

如果您不想使用toogleClass(),这是另一种解决方案:

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')

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

这篇关于如何使用jquery if else语句切换类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆