jQuery toggle()与toggleClass() [英] jQuery toggle() with toggleClass()

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

问题描述

我有一个按钮可以打开和关闭DIV图层.我正在尝试向按钮添加一个类,以便在其上进行切换时显示的外观有所不同,但是我无法使其正常工作,因此在切换按钮时未添加该类.

I have a button that toggles a DIV layer on and off. I am trying to add a class to the button so that when its toggled on it appears differently, however I haven't been able to get it working, the class is not being added when the button is toggled.

按钮是一个列表项,例如开/关

The button is a list item e.g. On / Off

这是我的代码:

$("#btninformation").click(function () {
  $("#map-items-category-one").toggle("slow", function() {
    $(this).toggleClass("toggled-on");
  });
});

任何想法有什么问题吗?

Any ideas whats wrong with it?

谢谢 扎克

推荐答案

您要在'#map-items-category-one'而不是'#btninformation'上切换类.只需在内部回调之外获取对按钮的引用即可:

You're toggling the class on the '#map-items-category-one', not on the '#btninformation'. Just grab a reference to the button outside of the inner callback:

$("#btninformation").click(function () {
  var $that = $(this);
  $("#map-items-category-one").toggle("slow", function() {
    $that.toggleClass("toggled-on");
  });
});

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

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