Javascript 在变量中存储背景色
如何将一个div的背景色存储在一个变量中,然后仅使用javascript使用该变量更改另一个div的背景色Javascript 在变量中存储背景色,javascript,Javascript,如何将一个div的背景色存储在一个变量中,然后仅使用javascript使用该变量更改另一个div的背景色 var b2 = document.getElementById("box2"), b3 = document.getElementById("box3"); function d2(){ document.getElementById("box3").style.backgroundColor = b2.style.backgroundColor; document.getElemen
var b2 = document.getElementById("box2"),
b3 = document.getElementById("box3");
function d2(){
document.getElementById("box3").style.backgroundColor = b2.style.backgroundColor;
document.getElementById("box2").style.backgroundColor = b3.style.backgroundColor;
}
我做了两个盒子,盒子2的颜色是红色,盒子3的颜色是绿色。然后尝试通过将颜色存储在变量中并将变量的颜色指定给div来交换颜色,但没有成功。任何帮助都将不胜感激。提前谢谢
这个代码对我来说很好用。谢谢你的帮助
function getStyle(x,styleProp) {
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
return y;
}
function d2(){
b2 = document.getElementById("box2")
b3 = document.getElementById("box3")
y1 = getStyle ( b3, 'background-color' )
document.getElementById("box3").style.backgroundColor = getStyle ( b2, 'background-color' )
document.getElementById("box2").style.backgroundColor = y1
}
为您创建了一把小提琴: JS:
单击以更改颜色
var color=$(“#d1”).css(“背景色”);
$(“#单击”)。单击(函数()
{
$(“#d2”).css(“背景色”,颜色);
})
只使用javascript
试试这个
var divColor = document.getElementById("id").style.backgroundColor;
//you get the color of div
然后使用以下命令将其应用于另一个div:
document.getElementById("id1").style.backgroundColor=divColor;
更新
<div id="box2">
<button type="button" class="d2" onClick="d2(this)">down</button>
</div>
<div id="box3">
</div>
function d2(element)
{
var c = window.getComputedStyle(element.parentElement).backgroundColor;
document.getElementById("box3").style.backgroundColor=c;
}
向下
功能d2(元素)
{
var c=window.getComputedStyle(element.parentElement).backgroundColor;
document.getElementById(“box3”).style.backgroundColor=c;
}
单击以更改颜色
var color=document.getElementById(“d1”).style.backgroundColor;
函数更改()
{
document.getElementById(“d2”).style.backgroundColor=color;
}
我将从了解如何获取div的背景色开始,然后使用该方法设置一个变量。e、 g.var color=$(div).css…
等等。但是没有任何证据,比如@PA.要求,我们能(或将)做的不多。谢谢PA和eric建议编辑。我是新来的,所以知道的不多。@user3755774。。。。我已经更新了fiddle try,oneOP没有要求jquery。OP没有要求jquery。谢谢。我还想知道我怎样才能互换两个div的颜色?谢谢。我还想知道我怎样才能互换两个div的颜色?你能为此制作一把小提琴吗?我也试过你的建议,但也不管用。这是一把小提琴:按照你的代码,我失去了第二个div的颜色。我的主要目的是交换它们的颜色。
document.getElementById("id1").style.backgroundColor=divColor;
<div id="box2">
<button type="button" class="d2" onClick="d2(this)">down</button>
</div>
<div id="box3">
</div>
function d2(element)
{
var c = window.getComputedStyle(element.parentElement).backgroundColor;
document.getElementById("box3").style.backgroundColor=c;
}
<div id="d1" style="width:100px;height:100px;border:1px solid;background-color:red"></div>
<div id="d2" style="width:100px;height:100px;border:1px solid;"></div>
<button id="click" type="button" onclick="change()">click to change the color</button>
<script>
var color=document.getElementById("d1").style.backgroundColor;
function change()
{
document.getElementById("d2").style.backgroundColor=color;
}
</script>