Jquery 递归获取元素的背景色失败
我试图得到一个网页中的标识的背景色。 我将要做的是克隆徽标,并将克隆放在另一个应该是正确颜色的div中 该文档看起来像:Jquery 递归获取元素的背景色失败,jquery,css,Jquery,Css,我试图得到一个网页中的标识的背景色。 我将要做的是克隆徽标,并将克隆放在另一个应该是正确颜色的div中 该文档看起来像: <div> <div class="foo"> <div id="start"> Find background for this div </div> </div> </div> <button onclick="on_find
<div>
<div class="foo">
<div id="start">
Find background for this div
</div>
</div>
</div>
<button onclick="on_find_color_clicked()">
find color
</button>
我得到这个错误:
NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 0 [nsIDOMWindow.getComputedStyle]
在
我希望这能有所帮助
您需要使用
.css(“背景色”)
而不是.css(“背景色”)
如果您想要颜色,您应该通过更改var value=element.css(“背景”)来要求它,而不是整个background
属性
tovar value=element.css('backgroundColor')
但更大的问题是函数为空或空(值){return value==null | | value==''''.}
应该是函数为空或空(值){return value==null | value=''rgba(0,0,0,0)}{value=='transparent'.}
因为空的背景色可以作为rgba返回(0,0,0,0)
或透明
。否则不会触发
类似这样的东西
function on_find_color_clicked() {
var background = get_background_of($('#start'));
alert("the background is " + background);
}
function get_background_of(element) {
var parent = element.parent();
var value = element.css('backgroundColor');
var result;
if (is_null_or_empty(value)) {
result = parent && parent != null ? get_background_of(parent) : "";
} else { result = value; }
return result;
}
function is_null_or_empty(value) {
return value == null || value == '' || value==='rgba(0, 0, 0, 0)' || value==='transparent';
}
胡闹:试试这个,它很好用普拉纳夫:是的,它很好用,回答这个评论,请我得到“背景是透明的”嗯…浏览器的差异?我在Chrome上,它返回“背景是rgb(255,0,255)”。使用firefox,有趣的是firefox中的“背景是透明的”。然而,我找到了一个解决方案
return window.getComputedStyle( elem, null );
function on_find_color_clicked() {
var background = get_background_of($('#start'));
alert("the background is " + background);
}
function get_background_of(element) {
var parent = element.parent();
var value = element.css('background-color');
if (value.indexOf("rgba(0, 0, 0, 0)") != -1) {
value = get_background_of(parent);
}
return value;
}
function on_find_color_clicked() {
var background = get_background_of($('#start'));
alert("the background is " + background);
}
function get_background_of(element) {
var parent = element.parent();
var value = element.css('backgroundColor');
var result;
if (is_null_or_empty(value)) {
result = parent && parent != null ? get_background_of(parent) : "";
} else { result = value; }
return result;
}
function is_null_or_empty(value) {
return value == null || value == '' || value==='rgba(0, 0, 0, 0)' || value==='transparent';
}