How to stop event propagation in jquery?

$("div#foobar").live('click', function() { }); $(window).hashchange(function() { }); 

How to stop the hashchange event when the click event fires.

Note In the click event, I fire ajax and change the hash ... And on hashchange, I do something. Since the Click event also changes the hash, it means hashevent fire. So, I want that whenever the event is clicked, the hashchange fire should not be fire.

+6
source share
3 answers
 (function() { var fire_hashchange = true; $("div#foobar").live('click', function() { fire_hashchange = false; // do stuff }); $(window).hashchange(function() { if (!fire_hashchange) { fire_hashchange = true; return; } // do stuff }); })(); 
0
source

stop distribution and prevent default behavior.

 $("div#foobar").live('click', function(event) { event.stopPropagation(); event.preventDefault() }); 
+10
source

Basically, you cannot do what you want. If you change the hash, of course, the hashchange event will fire. What is it there for. Preventing the click event from propagating will not prevent the hashchange event from being processed.

Build your system so that this is not a problem.

+1
source

Source: https://habr.com/ru/post/888366/


All Articles