Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 递归获取元素的背景色失败_Jquery_Css - Fatal编程技术网

Jquery 递归获取元素的背景色失败

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>
    <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
属性
to
var 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';
}