3 回答
TA贡献1815条经验 获得超12个赞
你可以这样做
import React from 'react'
export default function Numbers({ numbers }) {
const isPrime = num => {
for(let i = 2; i < num; i++)
if(num % i === 0) return false;
return num > 1;
}
const isOdd = (num)=> { return num % 2;}
const getBackGroundColor = (num)=>{
let color = 'red';
if(isOdd (num)) color ='red' //even
else color ='green' //odd
if(isPrime(num)) color = 'orange' //prime
return color ;
}
const list = numbers.map((number) =>
<div key={number} style={{backgroundColor: getBackGroundColor(number) }} className="numbers"><li className="list">{number}</li></div>
)
return list
}
TA贡献1887条经验 获得超5个赞
以下 css 使奇数背景为灰色,偶数背景为银色,素数背景为粉红色:
li:nth-child(2),
li:nth-child(odd) {
background: pink;
}
li:first-child,
li:nth-child(3n+6),
li:nth-child(5n+10),
li:nth-child(7n+14)
{
background: grey
}
li:nth-child(2n+4) {
background: silver
}
TA贡献1829条经验 获得超7个赞
这对我有用,我调用 random() 函数,它从数组中生成随机颜色。
const random = () => {
const backgroundColor = ["#134563", "#27ae60", "#3263F3", "#FFDC61"];
const randomColors = backgroundColor[Math.floor(Math.random() * backgroundColor.length + 0)];
return randomColors;
}
random();
添加回答
举报