【问题标题】:jQuery - Adding/removes classnames from the element and togling active class on the buttonjQuery - 从元素中添加/删除类名并在按钮上切换活动类
【发布时间】:2021-10-18 15:25:54
【问题描述】:

我需要将.important-element 更改为与按下同名按钮相同的颜色。我想通过将.red.blue.green 类名添加到.important-element 来实现这一点,如果按下了另一个或相同的按钮,也将它们删除。我不确定如何编辑我的 jQuery 代码以使其正常工作。

重要的事情:按下按钮后,它需要删除以前添加的类名(就像 jQuery 代码现在可以使用按钮一样)。换句话说:如果一个按钮已经处于活动状态,它需要在按下另一个按钮时从.important-element 中删除它添加的类名。

$('.btn').on("click", function() {
  $('button').not(this).removeClass('active');
  $(this).toggleClass('active');
})
.price-filter-container {width: 1190px;max-width:100%;
  margin: 0 auto;}
button {outline: 0; /* only to remove the ugly blue outline in this demo */}
button.active {color: white;}
button.green.active {background:green;}
button.red.active {background:red;}
button.blue.active {background:blue;}

.important-element {padding:20px; width:240px; text-align:center;  background:grey; margin-top:30px; color:#FFF;}
.important-element.green {background:green}
.important-element.red {background:red}
.important-element.blue {background:blue}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="price-filter-container">
    <button class="btn green">green</button>
    <button class="btn red">red</button>
    <button class="btn blue">blue</button>
</div>

<div class="important-element">i need to change with button click to the same collor as the button</div>

【问题讨论】:

    标签: jquery button toggle


    【解决方案1】:

    所以现在代码应该是正确的。

    $('.btn').on("click", function() {
      var classname = $(this).attr('class');
      var split = classname.split(" ");
      var elementcolor = split[1];
      var colorStack = ['green', 'red', 'blue'];
      $('button').not(this).removeClass('active');
      $(this).toggleClass('active');
      $.each($(colorStack), function(index, value) {
        if ($('.important-element').hasClass(value)) {
          $('.important-element').removeClass(value);
        }
      });
      $('.important-element').addClass(elementcolor);
      $.each($(colorStack), function(index, value) {
        if (!$('button').hasClass('active')) {
          $('.important-element').removeClass(value);
        }
      })
    });
    .price-filter-container {
      width: 1190px;
      max-width: 100%;
      margin: 0 auto;
    }
    
    button {
      outline: 0;
      /* only to remove the ugly blue outline in this demo */
    }
    
    button.active {
      color: white;
    }
    
    button.green.active {
      background: green;
    }
    
    button.red.active {
      background: red;
    }
    
    button.blue.active {
      background: blue;
    }
    
    .important-element {
      padding: 20px;
      width: 240px;
      text-align: center;
      background: grey;
      margin-top: 30px;
      color: #FFF;
    }
    
    .important-element.green {
      background: green
    }
    
    .important-element.red {
      background: red
    }
    
    .important-element.blue {
      background: blue
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="price-filter-container">
      <button class="btn green">green</button>
      <button class="btn red">red</button>
      <button class="btn blue">blue</button>
    </div>
    
    <div class="important-element">i need to change with button click to the same collor as the button</div>

    【讨论】:

    • 你用“风格”做到了,但我需要通过改变类名来做到这一点。你能帮忙吗?
    • 更新了我的代码示例。看看它是否符合您的期望。如果合适,请投票或将问题标记为已回答,这样我会得到一些积分:-) THX
    • 感谢您的尝试,但这段代码并没有做一件重要的事情。如果您单击任何按钮,例如“红色”和“重要元素”在您再次单击“红色”按钮后获取类“红色”,它应该从重要元素中删除类名“红色”,就像在我的初始代码中一样,但是它没有。
    • 再次更新了我的代码示例。请检查它是否符合您的需求。
    • 感谢您的检查。我让每个循环和三种颜色的数组变得更方便一些。希望对您有所帮助。
    【解决方案2】:

    稍微简化的版本:

    var colors = [ 'red', 'green', 'blue' ];
    $('.btn').on("click", function() {
      $(this).siblings('button').removeClass('active');
      $(this).addClass('active');
      var color = [ ...this.classList ].filter((className) => colors.includes(className))[0];
      var otherColors = colors.filter((colorName) => colorName !== color);
      $('.important-element').removeClass(otherColors.join(' ')).toggleClass(color);
    })
    .price-filter-container {width: 1190px;max-width:100%;
      margin: 0 auto;}
    button {outline: 0; /* only to remove the ugly blue outline in this demo */}
    button.active {color: white;}
    button.green.active {background:green;}
    button.red.active {background:red;}
    button.blue.active {background:blue;}
    
    .important-element {padding:20px; width:240px; text-align:center;  background:grey; margin-top:30px; color:#FFF;}
    .important-element.green {background:green}
    .important-element.red {background:red}
    .important-element.blue {background:blue}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="price-filter-container">
        <button class="btn green">green</button>
        <button class="btn red">red</button>
        <button class="btn blue">blue</button>
    </div>
    
    <div class="important-element">i need to change with button click to the same collor as the button</div>

    如果您喜欢使用data-* attributes,那可能会更容易:

    var colors = [ 'red', 'green', 'blue' ];
    $('.btn').on("click", function() {
      $(this).siblings('button').removeClass('active');
      $(this).addClass('active');
      var color = this.dataset.color;
      var otherColors = colors.filter((colorName) => colorName !== color);
      $('.important-element').removeClass(otherColors.join(' ')).toggleClass(color);
    })
    .price-filter-container {width: 1190px;max-width:100%;
      margin: 0 auto;}
    button {outline: 0; /* only to remove the ugly blue outline in this demo */}
    button.active {color: white;}
    button[data-color="green"].active {background:green;}
    button[data-color="red"].active {background:red;}
    button[data-color="blue"].active {background:blue;}
    
    .important-element {padding:20px; width:240px; text-align:center;  background:grey; margin-top:30px; color:#FFF;}
    .important-element.green {background:green}
    .important-element.red {background:red}
    .important-element.blue {background:blue}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="price-filter-container">
        <button class="btn" data-color="green">green</button>
        <button class="btn" data-color="red">red</button>
        <button class="btn" data-color="blue">blue</button>
    </div>
    
    <div class="important-element">i need to change with button click to the same collor as the button</div>

    【讨论】:

    • 感谢您的尝试,但这段代码并没有做一件重要的事情。如果您单击任何按钮,例如“红色”和“重要元素”在您再次单击“红色”按钮后获取类“红色”,它应该从重要元素中删除类名“红色”,就像在我的初始代码中一样,但是它没有。
    • 我实现了该要求并相应地更新了我的答案。
    • 你做到了。问题是我的问题更像是一个示例,这里的颜色仅用于示例目的。然而,您的代码仅适用于颜色,并且仅适用于这些特定的类名。我试图在这个问题上更准确,如果你可以看看:stackoverflow.com/questions/68818673/…
    • 请重新打开我刚刚关闭的问题。当我发布它并为您输入回复时。
    • 你可以在这个数组中放任何东西。它只是用来区分你想要的类和你不想要的类(例如.btn
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多