在单击jQuery的类之间切换 [英] Toggle between classes on click jQuery

查看:46
本文介绍了在单击jQuery的类之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有smallimage类的div-当我单击smallimage时,我想删除class smallimage并添加一个名为bigimage的类.

I have a div with the class smallimage - when I click on smallimage I want to remove the class smallimage and add a class called bigimage.

在下面的代码中,当我单击div时,它会删除smallimage并添加bigimage,但是当我再次单击时,它并不会删除bigimage并添加回smallimage.我想点击在小图片和大图片类之间切换.

With the code below when I click on the div it removes smallimage and adds bigimage, but when I click again it doesn't remove bigimage and add back smallimage. I want to toggle between the class smallimage and bigimage on click.

提前谢谢!

<script>
  $('.smallimage').click(function() {
    $(this).addClass('bigimage');
    $(this).removeClass('smallimage');
  });

  $('.bigimage').click(function() {
    $(this).addClass('smallimage');
    $(this).removeClass('bigimage');
  });
</script>

推荐答案

尝试以下操作.

$(document).on('click','.smallimage, .bigimage', function() {
   $(this).toggleClass('smallimage bigimage');
});

JSFiddle

这篇关于在单击jQuery的类之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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