Created
February 6, 2020 21:40
-
-
Save shilovk/fdf10bf9fdb45055f81346a4f7a5dad7 to your computer and use it in GitHub Desktop.
preventDefault() stopPropagation() и stopImmediatePropagation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ ================= preventDefault() ================= | |
// В примере сработали: event.preventDefault() и event.stopPropagation(). | |
$('a').click(function () { | |
return false; | |
}); | |
// Если вы всего лишь хотите предотвратить действие браузера по умолчанию, то вам следует использовать preventDefault метод. | |
$('a').click(function (event) { | |
event.preventDefault(); | |
}); | |
// ================= stopPropagation() ================= | |
/* Имеем следующую структуру: | |
<div id="demo"> | |
<a href="js4.it">Link</a> | |
</div> | |
*/ | |
$('a').click(function(event){ | |
event.preventDefault(); | |
event.stopPropagation(); | |
console.log('You have clicked the link.'); | |
}); | |
// Этот обработчик не будет вызван. | |
$('#demo').click(function(){ | |
$(this).toggleClass('yellow'); | |
console.log('You have clicked the demo div.'); | |
}); | |
/* | |
stopImmediatePropagation() останавливает не только всплытие события по родительским элементам, | |
но также останавливает работу всех последующих обработчиков конкретного события на данном элементе. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment