Jquery::在嵌套中调用函数两次<;部门>;?

Jquery::在嵌套中调用函数两次<;部门>;?,jquery,function,Jquery,Function,我有以下代码: <div id="parent1"> <div id="child1"></div> <!-- Which is a close button --> </div> 现在的问题是,当我单击“关闭按钮”时,它会调用这两个函数,并且不会将宽度恢复到原始宽度。如何修复此问题?尝试以下方法: $("#child1").click(function(event){ event.stopPropagation();

我有以下代码:

<div id="parent1">
    <div id="child1"></div> <!-- Which is a close button -->
</div>
现在的问题是,当我单击“关闭按钮”时,它会调用这两个函数,并且不会将宽度恢复到原始宽度。如何修复此问题?

尝试以下方法:

$("#child1").click(function(event){
    event.stopPropagation();
    // Stop the animation
    $("#parent1").stop();

    // restore the width to original one: 320px
})

该事件当前正在DOM树中冒泡出现。

单击#child1恢复关闭按钮的宽度

使用或可防止事件冒泡

示例-

$("#child1").click(function(event){
    event.stopImmediatePropagation();
    // Stop the animation
    $("#parent1").stop();
    // restore the width to original one: 320px
})
演示-

$("#child1").click(function(event){
    $("#parent1").stop();
    $("#parent1").css("width","320px");
    //or
    //event.stopPropagation();
})
$("#child1").click(function(event){
    event.stopImmediatePropagation();
    // Stop the animation
    $("#parent1").stop();
    // restore the width to original one: 320px
})