在點擊事件變化,每列的背景顏色和它的作品。但是,我想要具有不同的(僅不透明)html身體背景顏色。現在它爲body.background(最後一行)提供與其他列相同的顏色。相反,我希望身體具有相同的顏色,但0.5不透明度,因此頂部具有不透明度1的元素會很好地突出。Javascript onclick,如何對某些元素使用不同的alpha?
function BackgroundColorChange(){
// the main opacity level
var a = 1;
// some colors chosen for DOM elements backgrounds
var blue = 'rgba(51, 102, 153,'+a+')';
var grey = 'rgba(66, 82, 102,'+a+')';
var darkgreen = 'rgba(25, 102, 102,'+a+')';
var lightgreen = 'rgba(62, 116, 89,'+a+')';
var brown = 'rgba(96, 86, 57,'+a+')';
var purple = 'rgba(66, 36, 51,'+a+')';
var colorSelection = [blue, grey, darkgreen, lightgreen, brown, purple];
// pick the random color for background as the event is clicked
var color = colorSelection[Math.floor(Math.random() * colorSelection.length)];
// the elements i want to have 'opacity = 1' are selected here
var subsection = document.querySelectorAll(".childElement-column-photo, .childElement-sub-section, .sub-section, .modal-header, .modal-footer");
for (var i = 0; i < subsection.length; i++) {
subsection[i].style.backgroundColor = color;
}
a = 0.5; //? ? ? ? ?
// I want this body element to have the same color but with 0.5 opacity
document.body.style.backgroundColor = color;
}
我試圖在爲不透明度定義'a'時玩弄,但它沒有奏效。
HTML: 這是我的HTML主要是如何標記。
<a onclick="BackgroundColorChange();" href="#">ChangeColor</a>
<body onload="setInterval(BackgroundColorChange(), 50000)">
// there are many elements with this class name
<div class="childElement-column-photo">
可以置HTML的一點點請,只是想看看它是如何相互作用的。 – bhansa
你的'setInterval'調用實際上不起作用;你沒有傳遞任何東西。 – SLaks