问题描述
默认情况下,大多数网络浏览器会将页面呈现为白色背景。但是,这在某种程度上是用户可定制的,并且某些浏览器是不同的。因此,我想找到一种通过CSS或JavaScript找出页面背景颜色的方法。 Mozilla网站上的文档建议可以使用document.bgColor,其默认值为白色。它还建议不要使用它,因为它已被弃用。但是文档似乎与观察到的行为冲突:如果页面没有CSS来更改它,则document.bgColor是一个空字符串。建议的替代方法也不起作用:我尝试的所有操作都给了我一个空字符串或透明字符,这显然是错误的:我无法看到浏览器下方的桌面,因此它不是透明的。 (顺便说一句,IE11的行为实际上就像Mozilla的文档所说的Firefox一样。去吧。)
Most web browsers, by default, render pages as having a white background. However, this is to some extent user customizable, and some browsers are different. So, I want to find a way, either through CSS or JavaScript, to find out the background color of the page. The documentation on Mozilla's website suggests that document.bgColor can be used, and that its default value is white. It also suggests to not use it, since it's deprecated. But the docs seem to be in conflict with observed behavior: document.bgColor is an empty string if the page has no CSS to change it. The alternatives suggested don't work either: everything I tried gives me either an empty string or "transparent", which is clearly wrong: I can not see the desktop beneath my browser, hence it is not transparent. (Incidentally, IE11 actually behaves like Mozilla's documentation says that Firefox does. Go figure.)
我想创建一个html列表元素(< ul>
),其背景颜色与文档的背景颜色匹配。这可能吗? (我想您可能会想问:我是否想要它与背景匹配,不是透明我想要的?不是。我希望它掩盖其他一些元素。为什么?因为我将其中之一
I want to create an html list element (<ul>
) whose background color matches the background color of the document. Is this possible? (I suppose you might be tempted to ask: if I want it to match the background, isn't "transparent" what I want? No. I want it to cover up some other element. Why? Because I'm making one of those auto-suggest thingies.)
编辑:2个人明智地建议我添加一个示例,这样可以清楚地知道我在说什么。根据我收到的答案,这两个人绝对正确。我在其中一个答案的注释中添加了指向小提琴的链接,现在我在这里添加它:
2 people have wisely suggested that I add an example so it becomes clear what on earth I'm talking about. Based on the answers I've been receiving, these 2 people are absolutely right. I've added a link to a fiddle in the comments of one of the answers, and now I'm adding it here:
推荐答案
您可以使用-请注意,CSS3中已弃用这些颜色,建议改用外观属性。
You could use CSS2 system colors - note that these are deprecated in CSS3 and appearance property is advised to use instead.
/ *这应该是桌面背景* /
ul {background-color:Window; } / *这是浏览器背景* /
这篇关于获取文档的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!