4Manuals

  • PDF Cloud HOME

jQuery的开关不改变CSS(3色) Download

    具有Iframe表单的HTML文件,使用框架外的按钮通过JQuery提交表单按钮 CSS / JS-如何覆盖一个孩子而不是其他孩子的父母过渡 CSS在像素中设置多个颜色渐变? 创建一个没有固定布局的HTML表,设置最大宽度,并且行不换行但溢出:隐藏 转换后的SELECT下拉列表(以ul为单位)不会显示在SAFARI 在ajax调用成功之后添加文本区域 IntersectionObserver观察视口中的document.body上边缘 如何在网站上模拟JavaScript控制台? 如何在多步骤表单上启用浏览器后退和下一个箭头键 标签背景颜色混合

您看到我需要在此代码中进行哪些更改?

    <style>
    .bgColor1{background: red !important;}
    .bgColor2{background: blue !important;}
    .bgColor3{background: green !important;}
    </style>

    <button onclick="mySwitch()">SWITCH COLOR</button>
    <script>
    function mySwitch() {
       jQuery('.background').each(function(){
       var classes = ['bgColor1','bgColor2','bgColor3'];
       jQuery('.background').className = classes[($.inArray(jQuery('.background').className, classes)+1)%classes.length];
      });
    });
  </script>

以下仅适用于2种颜色的切换类别:

    <button onclick="jQuery('.background').toggleClass('bgColor2')">toggle bg</button>

但是我猜到toggleClass只支持2种颜色,而不是3种:(

1 个答案:

答案 0 :(得分:1)

您需要使用模运算符在类中循环。我已经制作了一个有效的示例here

HTML:

<div id="background" class="bgColor0">
  <button id="but">SWITCH COLOR</button>
</div>

JavaScript:

let counter = 0;
$('#but').click(function () {

  $('#background').removeClass('bgColor' + ((counter % 3))); // Remove the previous color's class
  $('#background').addClass('bgColor' + ((counter + 1) % 3)); // Add the new colors class

  counter++;

});

CSS:

#background {
  width: 200px;
  height: 200px;
}

.bgColor0{ background: red !important; }
.bgColor1{ background: blue !important; }
.bgColor2{ background: green !important; }



Similar searches
    五月份报表设计器中某月的天数 我如何将react-chartjs-2与chartjs-plugin-datasource集成以在s3网址上使用数据源 Windows Defender阻止我的愚蠢程序 Github-强制执行提交消息,例如bitbucket ChartJs Pointstyle图像仅在悬停时呈现