jQuery - 在div上单击事件但不在div上的复选框

Rya*_*yan 5 checkbox jquery click

我正在尝试在div上设置click事件,如果除了div中的复选框之外的任何地方点击了div,我希望触发该事件.如果单击该复选框,我不希望触发div的单击事件.我在复选框上设置了一个click事件并返回false,这会停止div的click事件,但也不允许复选框被选中.这就是我现在拥有的:

$('.theDiv').click(function() {
    // Click event for the div, I'm slide toggling something
    $('.sub-div', $(this)).slideToggle();
});

$('.checkboxInTheDiv').click(function() {
    // Do stuff for the checkbox click, but dont fire event above
    return false; // returning false won't check/uncheck the box when clicked
})
Run Code Online (Sandbox Code Playgroud)

tva*_*son 15

而不是返回false,这会阻止默认操作,请尝试停止传播.

$('.checkboxInTheDiv').click( function(e) {
    e.stopPropagation();
    ... other stuff...
    return true;
});
Run Code Online (Sandbox Code Playgroud)