gpt4 book ai didi

javascript - 如果 active = true 则在 javascript 中将其涂成红色

转载 作者:行者123 更新时间:2023-11-28 12:15:15 25 4
gpt4 key购买 nike

我需要检查 active = true 是否将文本涂成红色,但我做不到。

var products = [
{name: 'Коля', active: true},
{name: 'Вася', active: true},
{name: 'Петя', active: false},
];

var ul = document.createElement("ul");

for( var i = 0; i < products.length; i++ ){

var li = document.createElement("li");
li.innerHTML = products[i].name + " " + products[i].active;
if( products[i].active == true ){
products[i].name.style.color = "red";
}
ul.appendChild(li);
document.body.appendChild(ul);
}

最佳答案

使用li代替products元素

li.style.color = "red";

var products = [{
name: 'Коля',
active: true
},
{
name: 'Вася',
active: true
},
{
name: 'Петя',
active: false
},
];

var ul = document.createElement("ul");

for (var i = 0; i < products.length; i++) {

var li = document.createElement("li");
li.innerHTML = products[i].name + " " + products[i].active;
if (products[i].active == true) {
//products[i].name.style.color = "red";
li.style.color = "red";
}
ul.appendChild(li);
document.body.appendChild(ul);
}

关于javascript - 如果 active = true 则在 javascript 中将其涂成红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51017368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com