Events only bubble up. So the click event handler for the a
element is fired before the click event handler for the div
. If you want the behaviour you describe, the you need to add a click event handler to the a element which stops propagation to the div.
$("#myDiv a").click( function(event) {
event.stopPropagation();
} );
and keep whatever event handler you have on the div. This should allow the event to perform it's default action, and prevent the handler on the div being fired.
If you only want to prevent clicks on links then you can change your event handler for the div
element
$("#myDiv").click( function( event ) {
if( !$( event.target ).is( "a" ) )
{
// existing event handler
}
} );
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…