问题描述
我在元素中有一个元素,当我点击下面的元素时,我希望滑块打开。当我点击最外面的元素时,我希望滑块关闭。
I have an element inside an element, when I click the element underneath I want the slider to open. When I click on the outermost element I want the slider to close.
不幸的是,当我点击最外层时,它也会点击底层元素。有没有办法只点击最外面的元素忽略下面元素的点击?这些事件在点击时触发并使用javascript执行。
Unfortunately when I click on the outermost it clicks the underneath element as well. Is there a way to click only on the outermost element ignoring the click on the underneath element? The events are triggered on click and executed with javascript.
我尝试使用 z-index
但它仍然捕获了在元素下面也点击了,因为函数彼此相反没有任何反应。
I tried with z-index
but it still captures the underneath element clicked as well, and because the functions are contrary to one another nothing happens.
编辑:关于代码值1000字的提示
edit: on a "code is worth 1000 words" tip
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
})
}
因为代码是借来的
,我对此并不了解。但基本上我想要点击
: mousteente
r函数来点击,而不会干扰。 close()。点击
Because the code is borrowed
, I don't understand much of it. But basically I want the "click
" : "mousteente
r" function to work on click, without interfering with the .close().click
推荐答案
听起来你需要停止冒充DOM的点击事件被父元素捕获。您可以使用 stopPropagation()
来执行此操作:
It sounds like you need to stop the click event bubbling up the DOM to be caught by parent elements. You can use stopPropagation()
to do this:
$('.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);
})
这篇关于两个OnClick事件重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!