开发者

jQuery stop "click" action after first elevent

There are two nested elements, both have different click actions.

I need to stop outer element action when inner element is clicked.

HTML:

<div id='out'>
    <div id='in'></div>
</div>

jQuery:

$('#out').click(function(){alert('OUT div is pressed')})
$('#in').click(function(){alert('IN div is pressed')})

I need when in is pressed, only his action is executed. out's script should have no action.

开发者_StackOverflow社区

How it can be solved?


You should use stopPropagation():

$('#in').click(function(e){
   alert('IN div is pressed')
   e.stopPropagation();
});


event.stopPropogation() should be able to help you.


I think you should do this:

$('#out').click(function(){alert('OUT div is pressed')})
$('#in').click(function(event){
alert('IN div is pressed');
event.stopPropagation();
}
)

Look here for reference


$('#out').click(function(){
    alert('OUT div is pressed');
});
$('#in').click(function(){
    $('#out').unbind('click');
    alert('IN div is pressed');
});

or you could stop the propagation of the event, but you can't know for sure which event handler will be triggered first (capture vs bubble events) :

$('#in').click(function(e){
    e.stopPropagation();
    alert('IN div is pressed');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜