【发布时间】:2020-12-15 09:23:34
【问题描述】:
我有一个问题。所以,我有 3 个带背景的 div 和另外 3 个带边框的 div。我必须将带有红色背景的 div #1 移动到没有背景的 div 中。如果带有红色背景的 div 与带有红色边框的 div 匹配,则它应该以红色背景着色。请看代码。我被卡住了,因为我无法让 div 改变颜色。
let allDivs = document.querySelectorAll("#dd")
dd.addEventListener("click" , function(){
red.addEventListener("mousemove" , e=>{
red.style.left = e.clientX - 25 + "px"
red.style.top = e.clientY - 25 + "px"
})
blue.addEventListener("mousemove", e=>{
blue.style.left = e.clientX - 25 + "px"
blue.style.top = e.clientY - 25 + "px"
})
green.addEventListener("mousemove", e=>{
green.style.left = e.clientX - 25 + "px"
green.style.top = e.clientY - 25 + "px"
})
})
#dd{
width: 100%;
height: 100%;
display: inline-block;
}
#red{
width: 100px;
height: 100px;
border-radius: 50%;
background: red;
position: absolute;
}
#blue{
width: 100px;
height: 100px;
border-radius: 50%;
background: blue;
position: absolute;
}
#green{
width: 100px;
height: 100px;
border-radius: 50%;
background: green;
position: absolute;
}
#red1{
width: 100px;
height: 100px;
border-radius: 50%;
border:1px solid red;
margin-left: 300px;
}
#blue1{
width: 100px;
height: 100px;
border-radius: 50%;
border:1px solid blue;
margin-left: 300px;
}
#green1{
width: 100px;
height: 100px;
border-radius: 50%;
border:1px solid green;
margin-left: 300px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="dd">
<div id="red"></div>
<div id="red1"></div>
<div id="blue"></div>
<div id="blue1"></div>
<div id="green"></div>
<div id="green1"></div>
</div>
</body>
</html>
【问题讨论】:
-
不能用js吗?
-
什么意思?没看到JS吗?
-
您写道:如果带有红色背景的 div 与带有红色边框的 div 匹配,则它应该以红色背景着色。 nackground 是红色的,你想把它改成红色
-
@AramayisYeghiazaryan,你愿意接受 jQuery 解决方案吗?
-
Nikhil Patil nooo,只有香草 JS
标签: javascript html css