checkbox复选框和div click事件重叠,点击div后复选框也被选中,同时改变div颜色,否则则不选中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
div
{
display: inline-block;
width: 100px;
margin-left: 10px;
}
</style>
<script src="Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script>
//注册checkbox的click事件
$(document).on(‘click‘, ‘:checkbox‘, function (e) {
//停止事件冒泡,当点击的是checkbox时,就不执行父div的click
e.stopPropagation();
var oCk = $(this), parentDiv = oCk.parent();
oCk.prop(‘checked‘) ? parentDiv.css(‘background-color‘, ‘blue‘) : parentDiv.css(‘background-color‘, ‘‘);
});
//注册div的click事件,点击div时动态执行checkbox的click事件
$(document).on(‘click‘, ‘div‘, function () {
$(this).find(‘:checkbox‘).click();
})
</script>
</head>
<body>
<div>
<input type="checkbox" />A
</div>
<div>
<input type="checkbox" />B
</div>
<div>
<input type="checkbox" />C
</div>
</body>
</html>
原文地址:https://www.cnblogs.com/jiangjiali1228/p/10970105.html