从下面的代码片段中,我想在使用jQuery选中复选框时更改图标类。
<div class="container">
<div class="item-1">Checkbox code here</div>
<div class="item-2">Icons here (using Img tag)</div>
</div>
注意:容器类的外部div是重复的div。
如果您想更改班级名称,请选中此复选框。参见以下示例。
这里是您的HTML这样。
<div class="container">
<div class="item-1"><input type="Checkbox" name="1" class="check">1</div>
<div class="other-class item-2">Icons here (using Img tag)</div> <!-- You need to keep class name `other-class` for change the class name -->
</div>
这里是jQuery代码。
jQuery(document).ready(function(){
$( '.check' ).on('click', function(){
if( $(this).is(":checked") ) {
$('.container .other-class').removeClass('item-2').addClass('your-class-name');
} else {
$('.container .other-class').removeClass('your-class-name').addClass('item-2');
}
});
});
if($(input[type=checkbox]).is(":checked")){
//change class here
}