I have selected some elements like
a = document.querySelectorAll('.myClass');
Now I want to get the background color of each element. I try this
a[0].style.backgroundColor
it's work fine for me.
But
for( var x = 0; x< a.length; x++) {
var bc = [];
bc[x] = a[x].style.backgroundColor;
}
It's returning
Array(3) [ <2 empty slots>, "rgb(0, 0, 87)" ]
I am unable to understand what's wrong in loop.
Each time you go around the loop you overwrite bc
with a new, empty array.
Create that array once , before the loop.
you initialize the array inside the loop thats the problem
var bc = [];
for( var x = 0; x< a.length; x++) {
bc.push(a[x].style.backgroundColor);
}
请将 bc 变量移到循环之外。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.