检查背景颜色是否未定义? [英] Check if background-color is undefined?

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

问题描述

在以下情况下:

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

我将html元素的backgroundColor保存到一个变量。
如何查询bgCol是否未设置?我的页面上的各种元素甚至没有bgColor,所以我得到错误。

I'm saving the backgroundColor of html elements to a variable. How can I query if bgCol is not set? Various elements on my page do not even have a bgColor so I get errors.

如果我检查

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

我只是想知道这是否正确的东西检查?不应该是!=undefined!= 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

我测试rgba(0,0,0,0)因为我的控制台告诉我...如果我控制台。 log(bgCol),没有bgcolor设置我的控制台说:rgba(0,0,0,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)"

我只是想知道这是否正确我在做什么?

I just wonder if this is right what I'm doing?

推荐答案

这取决于浏览器 - 在主要浏览器中测试此值,并找出您应该测试的内容。

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.

编辑:
我做了 alert($(< div />\").css(\"background-color)),并返回。因此,我认为这是我的浏览器中的默认设置 - Chrome。

I did alert( $("<div />").css("background-color") ) and it returned "". So I think that is the default in my browser - Chrome.

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

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