【发布时间】:2017-12-31 18:11:35
【问题描述】:
我正在做一个 Chrome 扩展,我需要获取当前页面的背景颜色并将窗口的背景颜色更改为之前获得的颜色,我如何在 javascript 中做到这一点(有或没有 jQuery),如果需要html还是css?
【问题讨论】:
标签: javascript css google-chrome-extension window background-color
我正在做一个 Chrome 扩展,我需要获取当前页面的背景颜色并将窗口的背景颜色更改为之前获得的颜色,我如何在 javascript 中做到这一点(有或没有 jQuery),如果需要html还是css?
【问题讨论】:
标签: javascript css google-chrome-extension window background-color
你可以使用 DOMObject.style.backgroundColor 和 window.getComputedStyle:
<object to set background color of>.style.backgroundColor = window.getComputedStyle(document.getElementsByTagName(“BODY”)[0], null).getPropertyValue("background-color")
您可以在此处找到文档:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
【讨论】:
在同一个窗口中尝试这个活动:
<script src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<style>
body {
background: #279DDD;
}
</style>
<p>Click the button to put the new window in the current window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
debugger;
var bgColor=$('body').css('background');
var myWindow = window.open("", "_self");
myWindow.document.write("<p>I replaced the current window.</p>");
myWindow.document.body.style.background = bgColor;
}
</script>
或者这个在新窗口中
试试这个:
<script src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<style>
body {
background: #279DDD;
}
</style>
<p>Click the button to put the new window in the current window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
debugger;
var bgColor=$('body').css('background');
var myWindow = window.open("", "_self");
myWindow.document.write("<p>I new window.</p>");
myWindow.document.body.style.background = bgColor;
}
</script>
【讨论】: