javascript检查类是否存在,如果存在,则替换为相同的类

javascript检查类是否存在,如果存在,则替换为相同的类,javascript,Javascript,是的,标题里有很多我有这个剧本 $('.iconM-work').on('click', function () { $("#colorscreen").addClass("fadeInUpBig"); $("#colorscreen").css('background-color', 'rgba(164, 196, 0, 0.2)'); $(".tile-group.main").css({ marginLeft:"-40px", width: "108

是的,标题里有很多我有这个剧本

$('.iconM-work').on('click', function () {
     $("#colorscreen").addClass("fadeInUpBig"); 
      $("#colorscreen").css('background-color', 'rgba(164, 196, 0, 0.2)');
        $(".tile-group.main").css({ marginLeft:"-40px", width: "1080px"}).load("musability-musictherapy-company-overview.html");


});
但是我需要添加一些代码,如果类fadeInUpBig已经存在,那么在更改背景颜色之前,请执行以下操作(上面脚本的第二部分)

这里的任何建议都会非常有用。

请尝试下面的内容

if ($('.colorscreen').hasClass('fadeInUpBig')) {
  $(".fadeInUpBig").replaceWith($(".fadeInUpBig").clone());
}

如果
语句带有
.hasClass
$(“#colorscreen”).addClass(“fadeInUpBig”).css('background…','…')。relaceWith(…)
应该可以做到这一点。关于语法的任何想法,文档都很难解释。。。。因为我是一个白痴,而不是因为文档本身,所以基本上会检查类fadeinupbig是否存在,如果存在,则克隆它或重新加载它,或者用相同的类替换它,你会想到什么?给你:
if($(…).hasClass(…){$(…).replaceWith(…);}
。如果您不熟悉
If
语句的语法,我建议您阅读MDN JavaScript指南:,啊,谢谢我刚刚做了If($(“#colorscreen”).hasglass(“.fadeInUpBig”){$((.fadeInUpBig”)。替换为(.fadeInUpBig”);}。您认为呢?$(.fadeInUpBig)应该是$(.fadeInUpBig”)试试$(this.removeClass(.fadeInUpBig)).addClass(“fadeInUpBig”);也不起作用,只是跳过了,好像这个班已经存在了。。。。我的脚本现在是这样的$('.iconM-commentials')。on('click',function(){$(“.colorscreen”)。removeClass(“fadeInUpBig”)。addClass(“fadeInUpBig”);$(“.colorscreen”)。css('background-color',rgba(164196,0,0.2);$(.tile-group.main”)。css({marginLeft:“-40px”,width:“1080px”)。加载(“musability musictherapy company overview.html”);});
if ($('.colorscreen').hasClass('fadeInUpBig')) {
  $(".fadeInUpBig").replaceWith($(".fadeInUpBig").clone());
}