如何检查CSS背景颜色是否为白色? [英] How to check if the CSS background-color is white?

查看:430
本文介绍了如何检查CSS背景颜色是否为白色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这个:

$('*').each(function() {
        if($(this).css("background-color") == "#ffffff") {
           $(this).css("background-color") == "#000000"
        }
    });​​​​


推荐答案

即使它是正确的(它不是),它将是不可靠的,不可能工作。原因是有几种显示白色的方法:

Even if it were correct (it's not), it would be unreliable and unlikely to work. The reason for that is that there are several ways of showing a white colour:


  • white
  • > #ff 及其所有8种情况组合
  • rgb(255,255,255)值之间任意空格的组合

  • rgba(255,255,255,1)值之间的任意空格的组合

  • white
  • #ffffff and all 64 case combinations thereof
  • #fff and all 8 case combinations thereof
  • rgb(255,255,255) and all ∞ combinations of arbitrary whitespace between values
  • rgba(255,255,255,1) and all ∞ combinations of arbitrary whitespace between values

您可以这样检查:

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";

这篇关于如何检查CSS背景颜色是否为白色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆