[英]JavaScript; Background Color change and change back w/ same input
I made this code, but it wont work. 我做了这段代码,但它行不通。 Would be nice if some one can help me.
如果有人可以帮助我会很好。 :)
:)
I've tried many things already, but it always didn't worked. 我已经尝试了很多事情,但始终没有奏效。
I want it to be so that you press the button and the background gets light, when you press it again it goes back to the origin. 我希望它是这样,以便您按下按钮并且背景变亮,再次按下时它又回到原点。
<html>
<head>
<title>test</title>
<style>
.darkerBG {
background: #282828;
}
.lighterBG {
background: white;
}
</style>
</head>
<body class="darkerBG" id="bdbackground">
<script language="javascript" type="text/javascript">
function changeColor()
{
var bdBackground = document.getElementById('bdbackground').class;
if (document.getElementById('bdbackground').class == 'darkerBG')
{
document.getElementById('bdbackground').class == 'lighterBG';
} else if (document.getElementById('bdbackground').class == 'lighterBG')
{
document.getElementById('bdbackground').class = 'darkerBG';
}
}
</script>
<input type="button" class="button" value="test" onclick="changeColor()">
</body>
</html>
You must use className
rather than class
. 您必须使用
className
而不是class
。 class
is not a property on a DOM element . class
不是DOM元素的属性。
You need to modify the className
property on the element rather than class
. 您需要修改元素而不是
class
的className
属性。
For example: 例如:
document.getElementById('bdbackground').className = 'darkerBG';
In JavaScript, the correct property is .className
instead of .class
. 在JavaScript中,正确的属性是
.className
而不是.class
。 You also have an erroneous ==
in your assignment of document.getElementById('bdbackground').class == 'lighterBG';
您在分配
document.getElementById('bdbackground').class == 'lighterBG';
也有错误的==
document.getElementById('bdbackground').class == 'lighterBG';
Alternatively, consider using .classList.contains(...)
. 或者,考虑使用
.classList.contains(...)
。
<html> <head> <title>test</title> <style> .darkerBG { background: #282828; } .lighterBG { background: white; } </style> </head> <body class="darkerBG" id="bdbackground"> <script language="javascript" type="text/javascript"> function changeColor() { var myElement = document.getElementById('bdbackground'); if (myElement.className == 'darkerBG') { myElement.className = 'lighterBG'; } else if (myElement.className == 'lighterBG') { myElement.className = 'darkerBG'; } } </script> <input type="button" class="button" value="test" onclick="changeColor()"> </body> </html>
I have created a working solution for what you are trying to achieve. 我为您要实现的目标创建了一个可行的解决方案。 I created a JSBin with the solution and improved your code slightly: http://jsbin.com/botasehoci/1/edit?html,css,js,output
我使用该解决方案创建了一个JSBin,并稍微改进了您的代码: http ://jsbin.com/botasehoci/1/edit?html,css,js, output
(Edit, just noticed Quantastical answered with pretty much all the issues I brought up in the time it took me to to post this answer :/ ) (编辑,只是注意到Quantastical回答了我花了很多时间才提出的所有问题:/)
The main issues were that: 主要问题是:
bdbackground
on the button element. bdbackground
。 className
not class
className
而不是class
x == a
instead of x = a
) x == a
而不是x = a
) This is the updated JS: 这是更新的JS:
function changeColor() {
var button = document.getElementById('bdbackground');
var buttonClass = button.className;
if (buttonClass === 'darkerBG') {
button.className = 'lighterBG';
} else if (buttonClass === 'lighterBG') {
button.className = 'darkerBG';
}
}
This is the updated HTML for the button element: 这是button元素的更新的HTML:
<input id="bdbackground" type="button" class="lighterBG" value="test" onclick="changeColor()">
Use .classList.add()
and .classList.remove()
to toggle classes. 使用
.classList.add()
和.classList.remove()
切换类。 You could use .classList.toggle()
but in certain circumstances the toggled states will get discombobulated if there is more than one toggler used at the same time. 您可以使用
.classList.toggle()
但是在某些情况下,如果同时使用多个切换器,切换状态将变得混乱。
When the function changeColor()
is called: 调用函数
changeColor()
:
getElementById()
getElementById()
获取对正文的引用 .darkerBG
by using .classList.contains()
.classList.contains()
来查找主体是否具有名为.darkerBG
的类 .darkerBG
as a class: .darkerBG
作为类:
.lighterBG
.lighterBG
.lighterBG
and ... .lighterBG
并... .darkerBG
.darkerBG
function changeColor() { var bG = document.getElementById('bdbackground'); if (bG.classList.contains('darkerBG')) { bG.classList.remove('darkerBG'); bG.classList.add('lighterBG'); } else { bG.classList.add('darkerBG'); bG.classList.remove('lighterBG'); } }
.darkerBG { background: rgba(20, 10, 20, .6); transition: all 2s; } .lighterBG { background: rgba(200, 100, 200, .6); transition: all 2s; }
<html> <head> <title>test</title> </head> <body class="darkerBG" id="bdbackground"> <input type="button" class="button" value="test" onclick="changeColor()"> </body> </html>
Instead of using class
you can use className
or classList
with add()
/ remove()
or toggle()
. 除了使用
class
还可以将className
或classList
与add()
/ remove()
或toggle()
。
To check if class exists or not, you can get element attribute (class) by getAttribute('class')
, then search in it using indexOf()
by your class string. 要检查类是否存在,可以通过
getAttribute('class')
获得元素属性(类getAttribute('class')
,然后使用类字符串的indexOf()
对其进行搜索。
For best practice try to avoid inline events and use addEventListener
instead. 为了最佳实践,请尝试避免内联事件,而应使用
addEventListener
。
https://jsfiddle.net/5m5ck1fk https://jsfiddle.net/5m5ck1fk
//btn = document.querySelector('.button'); var btn = document.getElementsByClassName('button')[0], bdBackground = document.getElementById('bdbackground'); btn.addEventListener('click', function () { changeColor(); }); function changeColor() { if (bdBackground.getAttribute('class').indexOf('darkerBG') != -1) { bdBackground.classList.remove('darkerBG'); bdBackground.classList.add('lighterBG'); } else if (bdBackground.getAttribute('class').indexOf('lighterBG') != -1) { bdBackground.classList.remove('lighterBG'); bdBackground.classList.add('darkerBG'); } }
.darkerBG { background: #282828; } .lighterBG { background: white; }
<body class="darkerBG" id="bdbackground"> <input type="button" class="button" value="test"> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.