简体   繁体   English

单击第二次单击后删除添加的类

[英]click remove added class after second click

How to remove priSCW-active after second click. 第二次单击后如何删除priSCW-active

The code needs work but when i click second time on the container it is not closing. 代码需要工作,但是当我第二次在container上单击时,它没有关闭。

What i am missing here anyone can help me in this regard ? 我在这里想念的是谁能在这方面帮助我?

DEMO 演示

$(document).ready(function() {
   $("body").on("click", ".container", function(event) {
      event.stopPropagation();
      var ID = $(this).attr("id");
      $(".priSCWrap").removeClass("priSCW-active");
      $("#open" + ID).toggleClass("priSCW-active").siblings().removeClass('priSCW-active');;
   });
   $("body").click(function(){
         $(".priSCWrap").removeClass("priSCW-active");
    });
});
$(document).ready(function() {
   var clicked = 0;
   $("body").on("click", ".container", function(event) {
      if (++clicked === 2) {
          // your code
      }
      event.stopPropagation();
      var ID = $(this).attr("id");
      $(".priSCWrap").removeClass("priSCW-active");
      $("#open" + ID).toggleClass("priSCW-active").siblings().removeClass('priSCW-active');
   });
   $("body").click(function(){
         $(".priSCWrap").removeClass("priSCW-active");
    });
});

If you remove the removeClass() call, then the toggle will achieve what you need for you. 如果删除removeClass()调用,则切换将实现您所需的功能。 At the moment you remove the class and toggle will then always immediately add it back on again. 此刻,您删除该类,然后切换将始终立即再次将其重新添加。 Also note that the .priSCWrap elements are not siblings, so siblings() won't select anything. 还要注意, .priSCWrap元素不是兄弟姐妹,因此siblings()不会选择任何内容。 You need to query those elements again and remove the current from the matched set, to remove any existing priSCWrap-active classes. 您需要再次查询这些元素,并从匹配集中删除当前元素,以删除任何现有的priSCWrap-active类。 Try this: 尝试这个:

$("body").on("click", ".container", function(event) {
    event.stopPropagation();
    var $current = $(this).find('.priSCWrap').toggleClass("priSCW-active");
    $('.priSCWrap').not($current).removeClass('priSCW-active');
});

Example fiddle 小提琴的例子

Also note the use of this and find() which negates the need to bodge together an id selector. 还要注意,使用thisfind()不需要将id选择器结合在一起。

您必须删除此行

$(".priSCWrap").removeClass("priSCW-active");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM