全部。
我正在尝试创建一个数组,我可以在其中更改其中字体的颜色。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Array Font Colors</title>
</head>
<body>
<script type="application/javascript">
var colors= new Array("Black", "White", "Yellow");
for(var a=0; a<colors.length; a++){
document.write(colors[a]+ "<br>");
}
</script>
</body>
</html>
上面是我制作数组并显示它的内容。我不确定如何将每个元素的颜色更改为其给定的数组值。例如,由于第二个数组元素值为 White,因此将字体颜色设置为白色。我是 JavaScript 的新手,对这是如何完成的有点困惑。
谢谢
最佳答案
将 css 应用于 document.write
<html>
<head>
<meta charset="utf-8">
<title>Array Font Colors</title>
</head>
<body>
<script type="application/javascript">
var colors= new Array("Black", "White", "Yellow");
for(var a=0; a<colors.length; a++){
document.write("<p style='color:"+colors[a]+"'>"+colors[a]+ "</p>");
}
</script>
</body>
</html>
关于javascript - 更改数组中特定元素的字体颜色 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001972/