gpt4 book ai didi

javascript - 更改复选框列表项的颜色 onChange

转载 作者:太空宇宙 更新时间:2023-11-03 21:34:28 27 4
gpt4 key购买 nike

我有一个复选框 + 标签列表。我希望在选中复选框时更改列表项的背景颜色。目前,当选中复选框时,整个列表会更改背景颜色,而我只想更改单个元素的背景颜色。感谢您的帮助!

代码:

names = ["Dave","Bob","Chuck"];
var numberOf = names.length;
//Log the number of players and their names to the console.
console.log("You have " + numberOf + " recent players, and their names are " + names);
//Players List
var text = "<ul>";
for (i = 0; i < numberOf; i++) {
text += "<li class='playerListItem'><label><input type='checkbox' class='playerCheckbox'>" + names[i] + "</label></li>";
}
text += "</ul>";
document.getElementById("recentPlayersContainer").innerHTML = text;

//Changes background of currently selected playe
$('input.playerCheckbox').on('change', function(event) {
$('li.playerListItem').css('backgroundColor', 'rgba(255,102,51,0.15)');
});

});

最佳答案

$('input.playerCheckbox').on('change', function(event) {
$(this).closest('li.playerListItem').css('backgroundColor', 'rgba(255,102,51,0.15)');
});

关于javascript - 更改复选框列表项的颜色 onChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27492320/

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