gpt4 book ai didi

javascript - 如果选中复选框,则更改类 bg

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

我正在制作我的投资组合网站,需要一些 jQuery 代码方面的帮助。我想要一个父类对其中的复选框使用react。在这种情况下,当复选框处于事件/选中状态时,背景颜色需要更改为 #000

我不知道要添加哪一行代码才能使我的类对复选框使用react。我在谷歌上搜索了如何去做,但并没有变得更聪明。这可能是一件非常小的事情,但我希望能得到你们的帮助。

$("input[type='checkbox']").change(function() {
if ($(this).is(":checked")) {
$(this).parent();
} else {
$(this).parent();
}
});
.product {
background-color: #ccc;
/* needs to change to #000 when active */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<div class="product">
<input class="check" type="checkbox" name="logo"> Logo
</div>

最佳答案

基本上,您为事件状态创建第二个(更具体的)css 选择器,然后使用 jQuery 根据复选框值切换该状态/类。

https://jsbin.com/betafupogu/1/edit?html,css,js,output

CSS

.product {
background-color: #ccc; /* needs to change to #000 when active */
}

.product.active {
background-color: #000;
}

jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>

$("input[type='checkbox']").change(function(){
if($(this).is(":checked")){
$(this).parent().addClass('active');
}else{
$(this).parent().removeClass('active');
}
});

</script>

关于javascript - 如果选中复选框,则更改类 bg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55831092/

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