如何检查CSS背景颜色是否为白色?

use*_*659 1 javascript css jquery colors

我需要这个:

\n\n
$(\'*\').each(function() {\n        if($(this).css("background-color") == "#ffffff") {\n           $(this).css("background-color") == "#000000"\n        }\n    });\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n

在类的单击上工作。

\n

Nie*_*sol 5

即使它是正确的(事实并非如此),它也是不可靠的并且不太可能起作用。原因是有几种显示白色的方法:

  • white
  • #ffffff及其全部 64 种情况组合
  • #fff及其全部 8 种情况组合
  • rgb(255,255,255)以及值之间任意空白的所有 ∞ 组合
  • rgba(255,255,255,1)以及值之间任意空白的所有 ∞ 组合

你可以这样检查:

if( $(this).css("background-color").match(/^(?:white|#fff(?:fff)?|rgba?\(\s*255\s*,\s*255\s*,\s*255\s*(?:,\s*1\s*)?\))$/i))
    this.style.backgroundColor = "#000";
Run Code Online (Sandbox Code Playgroud)