[英]How to change background color of multiple link using javascript?
I want to change the background-color of links according to the given array colors
using javascript and mainly using for loop and the parent-child selector of javascript/jquery. 我想使用javascript根据给定的数组colors
更改链接的背景颜色,并且主要使用for循环和javascript / jquery的父子选择器。 When I will reach the end of elements of colors array, I'll start with the first element of colors array. 当我到达颜色数组元素的末尾时,我将从颜色数组的第一个元素开始。
Array for color var colors = ['#607ec7', '#ca85ca', '#61c436', '#e54e7e', '#f4b23f', '#46c49c'];
var colors = ['#607ec7', '#ca85ca', '#61c436', '#e54e7e', '#f4b23f', '#46c49c'];
数组var colors = ['#607ec7', '#ca85ca', '#61c436', '#e54e7e', '#f4b23f', '#46c49c'];
<div class="vce-featured-section"> <a href="#" class="category-97">ReraFirst Updates</a> <a href="#" class="category-97">ReraFirst Updates</a> <a href="#" class="category-97">ReraFirst Updates</a> <a href="#" class="category-97">ReraFirst Updates</a> <a href="#" class="category-97">ReraFirst Updates</a> <a href="#" class="category-97">ReraFirst Updates</a> <a href="#" class="category-97">ReraFirst Updates</a> <a href="#" class="category-97">ReraFirst Updates</a> <a href="#" class="category-97">ReraFirst Updates</a> </div>
Use a while
loop and for circular traversing of color
array use colors[i % colors.length]
: 使用while
循环,对于color
数组的循环遍历,使用colors[i % colors.length]
:
var colors = ['#607ec7', '#ca85ca', '#61c436', '#e54e7e', '#f4b23f', '#46c49c']; var i=0; var aElem = $('.vce-featured-section a'); while(i !== aElem.length){ $(aElem[i]).css('background', colors[i % colors.length]); i++; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="vce-featured-section"> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a> </div>
USING PURE JS 使用PURE JS
var colors = ['#607ec7', '#ca85ca', '#61c436', '#e54e7e', '#f4b23f', '#46c49c']; var i=0; var aElem = document.querySelectorAll('.vce-featured-section a'); while(i !== aElem.length){ aElem[i].style.background = colors[i % colors.length]; i++; }
<div class="vce-featured-section"> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a><br> <a href="#" class="category-97">ReraFirst Updates</a> </div>
var colors = ['#607ec7', '#ca85ca', '#61c436', '#e54e7e', '#f4b23f', '#46c49c']; for (i = 0; i <= colors.length; i++) { var a = document.createElement("a"); var t = document.createTextNode("ReraFirst Updates" + i); a.setAttribute("style", "background:" + colors[i] + ""); a.setAttribute("href", "#"); a.setAttribute("class", "category-97") a.appendChild(t); var br = document.createElement("br"); document.body.appendChild(a); document.body.appendChild(br); }
You can use a simple .each loop to setup colors; 您可以使用简单的.each循环来设置颜色;
var colors = ['#607ec7', '#ca85ca', '#61c436', '#e54e7e', '#f4b23f', '#46c49c'];
var length = colors.length;
var currentColorIndex = 0;
$('.vce-featured-section a').each(function(index) {
$(this).css('background-color', colors[currentColorIndex]);
currentColorIndex = ++currentColorIndex % length;
});
<div class="vce-featured-section">
<a href="#" class="category-97">ReraFirst Updates</a>
<a href="#" class="category-97">ReraFirst Updates</a>
<a href="#" class="category-97">ReraFirst Updates</a>
<a href="#" class="category-97">ReraFirst Updates</a>
<a href="#" class="category-97">ReraFirst Updates</a>
<a href="#" class="category-97">ReraFirst Updates</a>
<a href="#" class="category-97">ReraFirst Updates</a>
<a href="#" class="category-97">ReraFirst Updates</a>
<a href="#" class="category-97">ReraFirst Updates</a>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.