如何在多个类别(超过2个)之间切换?

时间:2018-07-05 13:07:49

标签: jquery html css

我将尝试解释我想做什么。如果类别大于2,如何在多个类别之间切换?当我单击彩色按钮时,将添加/交换容器颜色类别。当我单击白色按钮时,它将删除容器颜色类别。

下面是一个JSFiddle示例,可以使您更好地理解:https://jsfiddle.net/bwd86h4e/1/

我希望有人能提供帮助。 谢谢。

HTML:

<div class="buttons">
  <div class="btn btn-reset"></div>
  <div class="btn btn-red"></div>
  <div class="btn btn-green"></div>
  <div class="btn btn-yellow"></div>
</div>

<div class="container">
  When I click on the colored buttons above, then container color class will exchanged. When I click on the white button, then it removes container color class. How to do it?
</div>

CSS:

/* Buttons */
.btn {
  display: inline-block;
  width: 30px;
  height: 30px;
  margin: 3px;
  cursor: pointer;
  border: 2px solid #333;
}
.btn.btn-red { background-color: red; }
.btn.btn-green { background-color: green; }
.btn.btn-yellow { background-color: yellow; }

/* Container */
.container {
  max-width: 480px;
  margin-top: 20px;
  padding: 40px;
  border: 2px solid #333;
}

/* Container color classes */
.container.cont-red { background-color: red; }
.container.cont-green { background-color: green; }
.container.cont-yellow { background-color: yellow; }

4 个答案:

答案 0 :(得分:4)

您使用jQuery:

$('.btn').click(function()
{
    var color = $(this).attr('data-class');
    //check to see if el has class btn-reset
    if ($(this).hasClass('btn-reset')) {
        $('.container').attr('class', 'container')
    } else {
        $('.container').attr('class', 'container cont-'+ class)
    }
})

,并且您的html更改为:

<div class="buttons">
  <div class="btn btn-reset" data-class="reset"></div>
  <div class="btn btn-red" data-class="red"></div>
  <div class="btn btn-green" data-class="green"></div>
  <div class="btn btn-yellow" data-class="yellow"></div>
</div>

参考:

https://api.jquery.com/attr/

https://api.jquery.com/addClass/

https://api.jquery.com/hasClass/

jsFiddle:https://jsfiddle.net/fkopmjyx/

答案 1 :(得分:3)

像这样的纯javascript:

var buttons = document.querySelectorAll(".btn");
var container = document.getElementById("container_1");
var container_color_classes = ["cont-red","cont-green","cont-yellow"];

buttons.forEach(function(button){
		button.addEventListener("click", function(){
  	container_color_classes.forEach(function(color){
    	container.classList.remove(color);
    })
    var className = this.getAttribute("data-class");
    if(className !== null)
    	container.classList.add(className);
  });
});
/* Buttons */
.btn {
  display: inline-block;
  width: 30px;
  height: 30px;
  margin: 3px;
  cursor: pointer;
  border: 2px solid #333;
}
.btn.btn-red { background-color: red; }
.btn.btn-green { background-color: green; }
.btn.btn-yellow { background-color: yellow; }

/* Container */
.container {
  max-width: 480px;
  margin-top: 20px;
  padding: 40px;
  border: 2px solid #333;
}

/* Container color classes */
.container.cont-red { background-color: red; }
.container.cont-green { background-color: green; }
.container.cont-yellow { background-color: yellow; }
<div class="buttons">
  <div class="btn btn-reset"></div>
  <div class="btn btn-red" data-class="cont-red"></div>
  <div class="btn btn-green" data-class="cont-green"></div>
  <div class="btn btn-yellow" data-class="cont-yellow"></div>
</div>

<div class="container" id="container_1">
  When I click on the colored buttons, then container color class will added/exchanged. When I click on the white button, then it removes container color class. <strong>How to do it?</strong>
</div>

答案 2 :(得分:0)

您可以使用添加类来切换类

document.addEventListener('keydown', (event) => {
  const keyName = event.key;
  if(keyName === 's')
      //save path to element here
});

https://jsfiddle.net/RACCH/0uwzgf9x/

答案 3 :(得分:0)

如果需要,您可以像这样使用JQuery:

<div class="container change-color">
    When I click on the colored buttons above, then container color class 
    will exchanged. When I click on the white button, then it removes 
    container color class. How to do it?
</div>

$('.change-color').click(function(){
    var theColorIs = $(this).css("background-color");
    $('.container').css('background-color', theColorIs);
});