jQuery toggle()方法的最佳替代方案? [英] Best alternative for jQuery toggle() method?

查看:90
本文介绍了jQuery toggle()方法的最佳替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery不推荐使用toggle()方法。这是轻松切换类的方法,在搜索stackoverflow时,我发现了各种其他方法来完成相同的任务(支持eventData的jQuery的.toggle()方法的替代?)。哪个jQuery代码段应该是切换此标记类的理想选择?

jQuery deprecated toggle() method. It was way to easy to toggle classes, While searching stackoverflow I found various other methods to accomplish the same task( Alternative to jQuery's .toggle() method that supports eventData? ). Which jQuery snippet should be ideal to switch classes for this markup?

谢谢

标记:

<a href="#">Toggle Class</a>
<div class="a"></div>

CSS:

.a{
background: #f2f2f2;
}
.b{
background: #ededed;
}


推荐答案

在这种情况下,只需使用 .toggleClass()

In this case, simply use .toggleClass().

$("a").click(function(){
    $("div").toggleClass("a b");
});

这篇关于jQuery toggle()方法的最佳替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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