2012-01-18 55 views
0

这是真的简单但我睡眠不足(新生儿),似乎无法得到这个权利。背景颜色转移

这很简单,我想要的是当单击div时,使用jQuery获取该div(var currentColor)的bg颜色并将其设置在另一个div上(无关系,不是父级或兄弟)。

这里是我的jQuery代码:

$("#bacon div").click(function(){ 
    var currentColor = $(this).css("background-color"); 
    $("#chosenColor").css("backgroundColor" , "currentColor"); 

}); 

和提琴是在这里:http://jsfiddle.net/dYhMp/

我在做什么错?我是否采取了错误的方法,或者我的脑子太油腻,不知道这里发生了什么? :)

回答

3

你只需要删除第二"currentColor"各地报价:

$("#bacon div").click(function(){ 
    var currentColor = $(this).css("background-color"); 
    $("#chosenColor").css("backgroundColor" , currentColor); 
}); 

http://jsfiddle.net/dYhMp/3/

+0

Omg,我知道这是因为我的大脑被炸,谢谢:) – Kyle 2012-01-18 13:02:45

2

你把引号之间的变量名。删除:

$("#chosenColor").css("backgroundColor" , currentColor); 
1

currentColor是含有色串的变量"currentColor"字符串本身。

$("#bacon div").click(function(){ 
    var currentColor = $(this).css("background-color"); 
    $("#chosenColor").css("backgroundColor" , currentColor); 
}); 
0

你有 “currentColor” 作为一个字符串,它修复了:

$("#chosenColor").css("backgroundColor" , currentColor); 

:)

0

设置CSS的时候,因为它是一个变量上currentColor删除双引号。我检查它,它的工作原理:

$("#bacon div").click(function(){ 
    var currentColor = $(this).css("background-color"); 
    $("#chosenColor").css("backgroundColor" , currentColor); 

});