我尝试做的是当复选框被选中时,改变div的背景颜色,而当它未被选中时,删除背景颜色。我如何使用jquery做到这一点呢?
<div class="checkbox-container">
<input type="checkbox" class ="checkbox-border" id="personal-info-checkbox">
<label for="personal-info-checkbox"> Mark as reviewed and acknowledged
</label>
</div>
使用parent selector和. removeclass我不知道如何使用jquery选择我的div并关闭和打开颜色。
5条答案
按热度按时间bogh5gae1#
您不需要jQuery来完成此操作
您只能使用css来执行此操作。
chromium、safari中支持
:has
伪类。对于firefox,需要启用标志。了解更多信息,请访问mdn::has pseudo class
cigdeys32#
将
change
事件侦听器添加到input
,该侦听器根据是否选中来设置其最接近的父级div
的背景颜色:第一个
9q78igpj3#
"试试这个,希望对你有帮助"
ergxz8rk4#
下面是一个示例,说明如何执行此操作
第一个
pbwdgjma5#
There are a number of ways to do this:
The below example has explanatory comments in the code:
References:
:has()
.:checked
.:has()
.var()
.document.querySelector()
.Element.classList
API .is()
.on()
.toggleClass()
.