简体   繁体   English

检查background-color是否未定义?

[英]Check if background-color is undefined?

In the following situation: 在以下情况下:

var bgCol = $(selector).css('backgroundColor');

I'm saving the backgroundColor of html elements to a variable. 我将html元素的backgroundColor保存到变量中。 How can I query if bgCol is not set? 如何查询是否未设置bgCol? Various elements on my page do not even have a bgColor so I get errors. 页面上的各种元素甚至都没有bgColor,因此出现错误。

If i check for 如果我检查

if ( bgCol != "rgba(0, 0, 0, 0)" )

everything works fine. 一切正常。 I just wonder if this is the right thing to check for? 我只是想知道这是否是正确的检查? Shouldn't it be something like != "undefined" or != 0 不应该是像!= "undefined"!= 0

I'm testing for "rgba(0, 0, 0, 0)" because my console told me to... if I console.log(bgCol) and no bgcolor is set my console says: "rgba(0, 0, 0, 0)" 我正在测试"rgba(0, 0, 0, 0)" 0,0,0,0) "rgba(0, 0, 0, 0)"因为我的控制台告诉我...如果我console.log(bgCol)并且未设置bgcolor,我的控制台会说: "rgba(0, 0, 0, 0)"

I just wonder if this is right what I'm doing? 我只是想知道这是对的吗?

While it does depend on the browser, that doesn't mean that you need to fight with browser inconsistencies and try to second-guess whatever strings every browser will return (both now and in the future ). 尽管它确实取决于浏览器,但这并不意味着需要与浏览器不一致作斗争,并尝试猜测每个浏览器将返回的字符串(现在和将来 )。 Right now, it's 'transparent' in IE and Firefox, and rgba(0, 0, 0, 0) in Webkit browsers, but this may change. 目前,它在IE和Firefox中是“透明的”,在Webkit浏览器中是rgba(0,0,0,0),但这可能会改变。

Instead of hard-coding strings and trusting browsers to give you what you expect, you can look up and test against the exact actual name this visitor's browser actually uses for the background-color of a dummy element with background:none; 您无需查找字符串,也不用信任浏览器来满足您的期望,而是可以查找并测试此访问者的浏览器实际使用的确切真实名称作为具有background:none;的虚拟元素的background-color background:none; .

// Get this browser's take on no fill. Append, else Chrome returns 'initial'  
var $temp = $('<div style="background:none;display:none;"/>').appendTo('body');
var transparent = $temp.css('backgroundColor');
$temp.remove();

// Use it
if ( $elem.css('backgroundColor') != transparent ) {
  // It's set
} else {
  // It's blank
}

Even if some crazy future browser chooses to use the name "Clive" for the background-color of elements where none has been set (or, more realistically, maybe something based on a schema that better works with the whole sRGB gamut or screen color profiles) this will still work. 即使某些疯狂的未来浏览器选择使用名称“ Clive”作为未设置任何元素的background-color (或更现实的是,也许基于某种模式的东西也可以更好地与整个sRGB色域或屏幕颜色配置文件配合使用) ),仍然可以使用。

Here's an example JSBIN in the context of answering the question How do I detect the inherited background-color of an element using jQuery/JS? 这是一个在回答问题的上下文中的示例JSBIN如何使用jQuery / JS检测元素的继承背景色?


Be aware though that some browsers are inconsistent with themselves depending on whether the element is part of the document body or not. 请注意,尽管某些浏览器与它们自身不一致这取决于该元素是否是文档主体的一部分。 So, make sure you compare like with like. 因此,请确保您比较喜欢。

For example, in Chrome: 例如,在Chrome中:

$('<div/>').css('backgroundColor'); 
// returns '' empty string 

$('<div style="background:none;"/>').css('backgroundColor'); 
// returns 'initial'

$('<div style="background:none;"/>').appendTo('body').css('backgroundColor'); 
// returns 'rgba(0, 0, 0, 0)' 

This depends on the browser - I'd test in for this value in major browsers and find out what you should be testing for. 这取决于浏览器-我会在主流浏览器中测试该值,然后找出您应该测试的内容。 Alternatively, you could check the web to see if this issue has been discussed before. 或者,您可以在网上查看该问题是否已经讨论过。

EDIT: I did alert( $("<div />").css("background-color") ) and it returned "" . 编辑:我做了alert( $("<div />").css("background-color") ) ,并且返回了"" So I think that is the default in my browser - Chrome. 因此,我认为这是我的浏览器中的默认设置-Chrome。

There is ALWAYS a background color set. 总是有背景色设置。 If you don't set it, the browser sets it by default. 如果未设置,则浏览器默认设置。 I think it's a fairly right assumption that the default bg is white, as long as we are talking about just body. 我认为默认bg为白色是正确的假设,只要我们谈论的只是body。

In case you are talking about other nodes, what you should try to find out is if the background-color property was fetched from a direct rule or if it was inherited 万一您在谈论其他节点,您应该尝试找出的是background-color属性是从直接规则中获取的还是它是inherited

$(selector[attribute]) returns the set of selectors that match the selector you choose AND that have the attribute listed defined. $(selector [attribute])返回与您选择的选择器相匹配的选择器集合,并定义了列出的属性。

using this, you can loop through the DOM elements that you're interested in, checking to see if they have a "background-color" attribute. 使用此功能,您可以遍历感兴趣的DOM元素,检查它们是否具有“ background-color”属性。 if they do, great, if not, you can set their background-color attributes to a default of your choice. 如果可以,则很好,如果不能,则可以将其background-color属性设置为您选择的默认值。

Hope this is helpful. 希望这会有所帮助。

Andy 安迪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM