正确使用jQuery切换? [英] The proper use of jQuery toggle?

查看:165
本文介绍了正确使用jQuery切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此 stackoverflow answer



<$ p $($ {code} $('#user_button')。switch(function(){
$(。hello)。addClass(active);
} ){
$(。hello)。removeClass(active);
});

使用jQuery .toggle 回调函数添加和删​​除类到一个元素。现在根据jQuery .toggle 文档 .toggle 最初用于在所选元素的可见性状态之间切换。



我做了一个jsfiddle演示: http: //jsfiddle.net/taheri/nYdng/



在我的示例中,它似乎只是在回调函数之间交替,和可见性 #user_button 本身未被切换。我在文档中缺少什么?可以切换用于仅在功能之间交替使用



谢谢!

解决方案

有两种切换方式... 这个一个。他们应该以不同的方式命名他们,以避免这种混乱。


In this stackoverflow answer

$('#user_button').toggle(function () {
    $(".hello").addClass("active");
}, function () {
    $(".hello").removeClass("active");
});

The jQuery .toggle callback function is used to add and remove classes to an element. Now according to the jQuery .toggle documentation, .toggle is originally used to switch between visibility states of the selected element.

I've made a jsfiddle demo: http://jsfiddle.net/taheri/nYdng/

In my example here it seems to only alternate between call back functions, and the visibility of the #user_button itself is not being toggled. What am i missing out in the documentation here? Can toggle be used to only alternate between functions?

Thank you!

解决方案

There are two toggle methods... this one and this one. They should have named them differently to avoid this confusion.

这篇关于正确使用jQuery切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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