-
Difference between event.stopPropagation and event.preventDefaultWeb/DOMBrowser 2019. 9. 28. 16:25
1. Overview
Let's distribute the difference between event.stopPropagation and event.preventDefault in DOM Event fine-grained.
2. Description
- stopPropagation
stops the event from bubbling up the event chain.
- preventDefault
prevents the default action the browser makes on that event.
3. Examples
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="foo"> <button id="but">button</button> </div>
$("#but").click(function (event) { //using preventDefault, only the browser's default action is stopped but the div's click handler still fires. event.preventDefault() }) $("#foo").click(function () { alert("parent click event fired!") }) $("#but").click(function (event) { // With stopPropagation, only the button's click handler is called while the div's click handler never fires. event.stopPropagation() }) $("#foo").click(function () { alert("parent click event fired!") })
4. References
'Web > DOMBrowser' 카테고리의 다른 글
Browser Rendering Process (0) 2020.02.23 Event bubbling and capturing (0) 2019.09.29 Event delegation (0) 2019.09.29