好吧,我是 Javascript 的新手,我想知道如何使背景颜色成为变量。具体来说,我有三个不同的框架,我希望侧面的两个框架根据第三个框架中访问的页面更改颜色。如何将这两个侧框架的背景颜色设置为可以由第三框架中的任何文档更改的变量?我一直在网上四处寻找,但我的搜索一直没有结果。
编辑- 或者,通过单击超链接来更改它的方法也可以满足我的目的。
编辑 2 - 与上一个问题一样,这是我正在尝试的另一种方法,虽然我有更多关于它的信息,但也不会产生太多运气: 在 javascript 中将背景颜色设置为变量第 2 部分
要使用 javascript 操作背景颜色属性,您需要:
//Set to red
document.getElementById("frame").style.backgroundColor="red";
//Save into variable
var color = document.getElementById("frame").style.backgroundColor;
var frameRef = document.getElementById("frameId");
//alternatively you can get all farmes in one array
//var textRef = document.querySelectorAll(".frameClass");
function changeBg(newColor){
frameRef.style.backgroundColor = newColor;
}
<button type="button" onclick="changeBg('red')" value="red">Red</button>
<button type="button" onclick="changeBg('blue')" value="blue">Blue</button>