以背景色為條件的 if 語句 (If statement with background color as condition)


問題描述

以背景色為條件的 if 語句 (If statement with background color as condition)

I have looked at other topics regarding if statements with background colors as conditions; however, have not found a viable answer. Whether I create an element as a variable beforehand, or use rgb or rgba, I get no results, and the if passes through straight to the else. 

var element = $("#ARCStatusBox3EQETD");
console.log($('#ARCStatusBox1EQETD').css('backgroundColor'));
    if(element.css('background‑color') == "rgb(220,20,60)") {
        $('#HomeStatus1').css("background‑color", "#dc143c");
    }
    else if ($('#ARCStatusBox2EQETD').css('background‑color') == '#daa520' || $('#ARCStatusBox2EQETD').css('background‑color') == '#daa520' || $('#ARCStatusBox1EQETD').css('background‑color') == '#daa520'){
        $('#HomeStatus1').css("background‑color", "#daa520");
    }
    else {// ($('#ARCStatusBox3EQETD').css('background‑color') == '#7cfc00' || $('#ARCStatusBox2EQETD').css('background‑color') == '#7cfc00' || $('#ARCStatusBox1EQETD').css('background‑color') == '#7cfc00'){
        $('#HomeStatus1').css("background‑color", "#7cfc00");
}

There is my code, it works neither as == hex code or rgb/rgba.

Any help with a solution is greatly appreciated.

‑‑‑‑‑

參考解法

方法 1:

Try

var element = $("#ARCStatusBox3EQETD");
    if(element.css('background‑color') == "rgb(220, 20, 60)") {
        $('#HomeStatus1').css("background‑color", "#dc143c");
    }
    else if (hexColor($('#ARCStatusBox2EQETD').css('background‑color')) == '#daa520' || hexColor($('#ARCStatusBox2EQETD').css('background‑color')) == '#daa520' || $('#ARCStatusBox1EQETD').css('background‑color') == '#daa520'){
        $('#HomeStatus1').css("background‑color", "#daa520");
    }
    else {// ($('#ARCStatusBox3EQETD').css('background‑color') == '#7cfc00' || hexColor($('#ARCStatusBox2EQETD').css('background‑color')) == '#7cfc00' || hexColor($('#ARCStatusBox1EQETD').css('background‑color')) == '#7cfc00'){
        $('#HomeStatus1').css("background‑color", "#7cfc00");
}

function hexColor(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    return '#' + parts.join('');
}​

Also note some browsers will return the rgb with spaces after the ,'s

方法 2:

Looks like this could be browser‑specific:

Found here:

  

Also, jQuery can equally interpret the CSS and DOM formatting of   multiple‑word properties. For example, jQuery understands and returns   the correct value for both .css('background‑color') and   .css('backgroundColor'). Different browsers may return CSS color   values that are logically but not textually equal, e.g., #FFF,   #ffffff, and rgb(255,255,255).

方法 3:

I'm not sure that you can compare computed colors. I think jQuery provides just a string recognition. And computed color relies on the browser implementation.

I think you can only compare two strings, I don't think you can fetch a bg color of an element and with security rely it being computed properly. It may be possible, but I think that for such functionality you would have to expand your implementation.

Because one browser could use one color format, other can be using other, and those are different strings.

方法 4:

You need spaces between the commas in your rgb string.

'rgb(255, 255, 255)'

Here's a working jsfiddle http://jsfiddle.net/Pvt5Z/8/

EDIT The above will work in FireFox and Chrome, however, IE8 returns whatever the css string for background‑color is instead of normalizing it to an 'rgb' string.

方法 5:

Part of the problem is that the colors have more than one string representation:

"white"
"rgb(255, 255, 255)"
"#FFFFFF"
"#FFF"

all represent the color white.

To be able to compare 2 colors you would need to get both of the two colors on a common ground, in the same format.

jQuery.css() when used to obtain a css color will always return the color string in RGB format.

You can use that feature to compare colors by always specifying the color with which you want to compare in RGB format also.

Next statement will be true regardless of which string representation of the color white the element has:

element.css("background‑color") == "rgb(255, 255, 255)"

You could also compare color value as hex by converting element.css("background‑color") to hex.

The answers for this question give a few methods of converting a RGB color to hex.

(by DeprecatedBotairyttonino.jLouis RicciRăzvan Flavius Panda)

參考文件

  1. If statement with background color as condition (CC BY‑SA 3.0/4.0)

#jquery #javascript #css #html






相關問題

讓 jQuery 與 Netscape 7 和 8 一起工作 (Getting jQuery to work with Netscape 7 and 8)

使用 Jquery 的 mvc3 搜索結果 (mvc3 search results with Jquery)

從嵌套的 jquery 函數返回一個值 (Return a value from nested jquery function)

Mencocokkan lebar divisi dengan jquery (Matching division widths with jquery)

無法在 jQuery AJAX 中多次生成點擊事件 (unable to generate click event more than once in jQuery AJAX)

使用雙引號格式並用逗號分隔元素數組 (Implode an element array with double quote format and separated by comma)

選擇不更新 (Select doesn't update)

chrome中帶有省略號的多行文本問題 (issue with multiline text with ellipsis in chrome)

AJAX/PHP/JS - 無法將頁面內容加載到容器中 (AJAX/PHP/JS - Cannot load page content into container)

使用 jQuery 將文本插入 textarea (Insert text into textarea with jQuery)

滾動到頁面底部,僅當用戶在 DOM 操作之前已經位於底部時 (Scroll to bottom of page, only if the user already was at the bottom before DOM manipulation)

如何設置單選按鈕的樣式,使其看起來像普通的可點擊按鈕? (How do I style a radio button to look like a normal clickable button?)







留言討論