简体   繁体   English

用javascript切换两种颜色的最佳方法?

[英]Best way to switch between two colours with javascript?

Javascript beginner here. Javascript初学者在这里。 I essentially want to make a simple switch. 我本质上想做一个简单的开关。 If an element is black, change it to white. 如果元素为黑色,请将其更改为白色。 If it is white, change it to black. 如果是白色,请将其更改为黑色。

 function changeClass() { if (document.getElementById('myButton').style.backgroundColor == "white") { document.getElementById('myButton').style.backgroundColor = "black"; } else { document.getElementById('myButton').style.backgroundColor = "white"; } } 
 <button class="normal" id="myButton" onclick='changeClass()' >Change Colour</button> 

This code is quite messy though. 这段代码很乱。 Is there a better way to do this? 有一个更好的方法吗?

Toggle a class: 切换课程:

function changeClass(){
  document.getElementById('myButton').classList.toggle("the-class");
}

where your CSS is: 你的CSS在哪里:

.the-class {
    background-color: black;
}

...assuming the element's normal background color is white. ...假设元素的正常背景颜色为白色。

More about classList here . 更多关于classList 信息 Support is good, but you may need a polyfill in older environments. 支持很好,但您可能需要在旧环境中使用polyfill。

Example: 例:

 function changeClass() { document.getElementById('myButton').classList.toggle("the-class"); } 
 .the-class { background-color: black; } 
 <button class="normal" id="myButton" onclick='changeClass()'>Change Colour</button> 

You can use classList.toggle() 你可以使用classList.toggle()

 document.querySelector('#myButton').addEventListener('click',e => { e.target.classList.toggle('black') }) 
 .black{ background:black } 
 <button class="normal" id="myButton">Change Colour</button> 

Use something like classList.toggle() 使用类似classList.toggle()东西

 function switchColor(){ document.getElementById("resultDiv").classList.toggle("toggle") } 
 .element { height: 100px; width: 100px; background-color: red; } .element.toggle{ background-color: blue !important; } 
 <button onclick="switchColor()">Click me</button> <div id="resultDiv" class="element toggle"></div> 

You can create some specific class in your css (say, .black class which contains a background-color: black; rule) and then attach/detach that class based on your condition. 您可以在css中创建一些特定的类(例如, .black类,其中包含background-color: black;规则),然后根据您的条件附加/分离该类。

Your DOM element (HTML tag) have a handy classList property, which can be treated as a list of classes attached to this DOM. 您的DOM元素(HTML标记)具有方便的classList属性,可以将其视为附加到此DOM的类列表。 I suggest to read a bit more about it here . 我建议在这里阅读更多相关内容。

Overall, your function can be written like: 总的来说,您的功能可以写成:

  const element = document.getElementById("coolDiv");
  element.classList.contains('black')) {
    element.classList.remove('black')
  } else {
    element.classList.add('black')
  }

or even a little more concise with a ternary operator 或者甚至用三元运算符更简洁一些

  const element = document.getElementById("coolDiv");
  element.classList.contains('black') ?
    element.classList.remove('black') : element.classList.add('black')

or just with a toggle function of the same classList property 或者只是具有相同classList属性的toggle功能

  const element = document.getElementById("coolDiv");
  element.classList.toggle('black')

Hope it helps! 希望能帮助到你! Cheers! 干杯!

and if white it's not the defualt color you can refactor using ? 如果白色不是你可以重构使用的defualt颜色? operator: 运营商:

let btn = document.getElementById('myButton');
btn.style.backgroundColor = btn.style.backgroundColor === 'white' ? 'black' : 'white';

For this action not needed external javascript you can write simple inline javascript here the code: 对于这个动作不需要外部JavaScript你可以在这里编写简单的内联javascript代码:

 .black-button { background: black; color: #fff; outline: 0; padding: 20px; } button { transition: 0.3s; cursor: pointer; } 
 <button class="normal" onclick="this.classList.toggle('black-button')">Change Colour</button> 

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

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