获取文档的背景颜色 [英] Get the document's background color

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

问题描述

大多数网络浏览器默认情况下会将网页呈现为白色背景。然而,这在某种程度上是用户可定制的,并且一些浏览器是不同的。所以,我想找到一种方式,通过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个人是绝对正确的。我在其中一个答案的评论中添加了一个链接,现在我将其添加到这里:

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:

https://jsfiddle.net/ftgu97fj/5/

推荐答案

您可以使用 CSS2系统颜色 - 请注意,这些在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: Background; /* CSS2 only; or maybe "Window", see @Larkeith comment */
}

这篇关于获取文档的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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