两个OnClick事件重叠

hah*_*aha 4 javascript jquery

我在元素中有一个元素,当我单击下面的元素时,我希望滑块打开.当我点击最外面的元素时,我希望滑块关闭.

不幸的是,当我点击最外层时,它也会点击底层元素.有没有办法只点击最外面的元素忽略下面元素的点击?这些事件在点击时触发并使用javascript执行.

我尝试过,z-index但它仍然捕获下面单击的元素,并且因为功能彼此相反,没有任何反应.

编辑:关于"代码值1000字"的提示

var $target = $(this).data('pos', i) //give each li an index #
    $target.data('hpaneloffsetw', $target.find('.hpanel:eq(0)').outerWidth()) //get offset width of each .hpanel DIV (config.dimensions.fullw + any DIV padding)
    $target[haccordion.ismobile? "click" : "mouseenter"](function(){
        haccordion.expandli(config.accordionid, this)
        config.$lastexpanded=$(this)
    })

    if (config.collapsecurrent){ //if previous content should be contracted when expanding current
        $('.close').click(function(){
            $target.stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
        })
        $target.dblclick(function(){
            $(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
        })
    }
Run Code Online (Sandbox Code Playgroud)

Because the code is borrowed,我不太了解它.但基本上我希望" click":" mousteenter"函数在点击时工作,而不会干扰.close().点击

Ror*_*san 8

听起来你需要停止冒充DOM的click事件才能被父元素捕获.你可以stopPropagation()这样做:

$('.close').click(function(e) {
    e.stopPropagation();
    $target.stop().animate({ width: config.paneldimensions.peekw }, config.speed);
})
$target.dblclick(function(e) {
    e.stopPropagation();
    $(this).stop().animate({ width: config.paneldimensions.peekw }, config.speed);
})
Run Code Online (Sandbox Code Playgroud)