javascript - 使用 javaScript 更改背景颜色(请不要使用 jQuery)?
问题描述
我希望我的 id 'fb' 的背景颜色在单击带有 id btn 1 的按钮时变为黑色,当我再次单击它时它会变回原始颜色,即 #3b5999?我想要 javaScript 答案,请不要 jquery。
document.getElementById("btn1").onclick=function(){
var a= document.getElementById("fb");
if(a.style.backgroundColor=="#3b5999")
{
a.style.backgroundColor="black";
}
if(a.style.borderColor!="#3b5999")
{
a.style.backgroundColor="#3b5999";
}
}
#fb{
background-color: #3b5999;
height: 100px;
color: white;
}
#insta{
background-color: #e4405f;
height: 100px;
color: white;
}
#Youtube{
background-color: #cd201f;
height: 100px;
color: white;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="fb">
<h3>Facebbok</h3>
</div>
<div id="insta">
<h3>Instagram</h3>
</div>
<div id="Youtube">
<h3>Youtube</h3>
</div>
<button id="btn1">Click Me for facebook</button>
<button id="btn2">Click Me for Youtube</button>
<button id="btn3">Click me for Instagram</button>
解决方案
我建议classList.toggle()
为此目的使用:
document.getElementById("btn1").onclick = function() {
var a = document.getElementById("fb");
a.classList.toggle("colored")
}
#fb {
background-color: #3b5999;
height: 100px;
color: white;
}
#fb.colored{
background-color:black;
}
#insta {
background-color: #e4405f;
height: 100px;
color: white;
}
#Youtube {
background-color: #cd201f;
height: 100px;
color: white;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="fb">
<h3>Facebbok</h3>
</div>
<div id="insta">
<h3>Instagram</h3>
</div>
<div id="Youtube">
<h3>Youtube</h3>
</div>
<button id="btn1">Click Me for facebook</button>
<button id="btn2">Click Me for Youtube</button>
<button id="btn3">Click me for Instagram</button>
但如果你想使用你的方法,
- 你写
borderColor
的不是backgroundColor
, 和 - 读取
backgroundColor
属性将导致rgb(59, 89, 153)
而不是#3b5999
和 - 读取
backgroundColor
属性只能读取内联样式定义:
document.getElementById("btn1").onclick = function() {
var a = document.getElementById("fb");
if(a.style.backgroundColor=="rgb(59, 89, 153)")
{
a.style.backgroundColor="black";
}
else if(a.style.backgroundColor!="rgb(59, 89, 153)")
{
a.style.backgroundColor="#3b5999";
}
}
#fb {
background-color: #3b5999;
height: 100px;
color: white;
}
#insta {
background-color: #e4405f;
height: 100px;
color: white;
}
#Youtube {
background-color: #cd201f;
height: 100px;
color: white;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="fb" style="background-color: #3b5999">
<h3>Facebbok</h3>
</div>
<div id="insta">
<h3>Instagram</h3>
</div>
<div id="Youtube">
<h3>Youtube</h3>
</div>
<button id="btn1">Click Me for facebook</button>
<button id="btn2">Click Me for Youtube</button>
<button id="btn3">Click me for Instagram</button>
推荐阅读
- html - 和
- google-sheets - 今天时间范围和日期的 Countif 代码是什么?谢谢
- python - 从 Qtablewidget 读取文本值并自动分配给 Qcombobox
- amazon-web-services - 如何在 AWS Lambda 函数中验证 API 密钥
- jquery - 带有来自 SQL asp 网络服务器端的数据的 jQuery DataTable 子行
- c# - 在 C# 中连接到 AWS Managed SFTP 服务器时 SSH.NET 超时
- php - 新文件的 PHP 结果使用
- python - 我怎样才能通过附加值以指数方式增加每个下一个对象部分?
- azure - Azure Service Fabric 本地部署导致 DnsService 错误:DnsService UDP 侦听器无法启动
- r - 在 R 中导入新的文件类型