可以覆盖浏览器的默认文本突出显示(选择)背景颜色,例如:
::selection {
background: #ffb7b7;
}
Run Code Online (Sandbox Code Playgroud)
并且颜色是浏览器/操作系统特定的.有没有办法使用JavaScript或Dart读取浏览器的默认值?
我是一个有复杂问题的 JS 新手。我想找出任何网页的背景颜色。问题是有时它是在 body 标签中定义的,有时是在内联或外部 css 中定义的,在一个更复杂的例子中,一些网站没有在 body 上设置背景颜色(在某些情况下它们也是如此),而是放置了一个 DIV 来覆盖整个页面。所以我的问题是我想找出整个视图的背景颜色。有人可以帮帮我吗。
我已经尝试在发布这个问题之前进行搜索,但我只找到了获取主体颜色而不是整个视图背景的方法,因此请不要将其标记为重复。
提前致谢
我有一个javascript函数,当用户单击div以获取网页的当前背景颜色(我的笔记本电脑上为白色)时调用该函数:
<div id="div1" onclick="checkbkcolor(id, 1)">Just testing the web page bk color
</div>
Run Code Online (Sandbox Code Playgroud)
这是checkbkcolor()函数:
<head>
<script type="text/javascript">
// DOES NOTHING SO I COMMENTED IT OUT: document.body.style.backgroundColor = "red";
function checkbkcolor(id, val)
{
// THIS RETURNS NOTHING FOR THE COLOR -- the alert box
// only says "the document.body.style.backgroundColor is: "
// and nothing else
alert("the document.body.style.backgroundColor is: "
+ document.body.style.backgroundColor);
document.body.style.backgroundColor = "red";
// The alert box now shows 'red' for the color name, *AND*
// the web page's background has …Run Code Online (Sandbox Code Playgroud)