Take a look today at the front-end bubbling and event default events how to handle
1.event.stoppropagation () method
This is the bubbling method of blocking events, not allowing the event to spread to Documen, but the default event will still be executed, and when you drop this method, if you click on a connection, the connection will remain open.
2.event.preventdefault () method
This is the method that blocks the default event, and calling this method is that the connection will not be opened, but bubbles will occur, and bubbling will pass to the parent element of the previous layer;
3.return false;
This method is more violent, he'll be a colleague. Blocking event bubbling also blocks default events, writes this code, the connection is not opened, the event is not passed to the parent of the previous layer, and it is understood that return false is equal to the simultaneous invocation of Event.stoppropagation () and Event.preventdefault ()
4. Let's take a look at a few sets of demos, using jquery for DOM manipulation
This is the HTML code, div inside a set of a tag, connect to Baidu
[HTML]View PlainCopyprint?
- <div class="Box1">
- <a href="http://www.baidu.com" target="_blank"></a>
- </div>
CSS code, a tag occupies 1/4 of the space of the parent element, the background color is red;
[HTML]View PlainCopyprint?
- . box1{
- height:200px;
- width:600px;
- margin:0 Auto;
- }
- . Box1 a{
- Display:block;
- height:50%;
- width:50%;
- background:red;
- }
Here is the JS code, the first kind
[HTML]View PlainCopyprint?
- Do not block event bubbling and default events
- $ (". Box1"). Click (function () {
- Console.log ("1")//Do not block event bubbling will print 1, page jump;
- });
The second Kind
[HTML]View PlainCopyprint?
- Stop bubbling
- $ (". Box1 a"). Click (Function (event) {
- Event.stoppropagation ();//will not print 1, but the page will jump;
- });
- $ (". Box1"). Click (function () {
- Console.log ("1")
- });
Third Kind
[HTML]View PlainCopy print?
- $ (". Box1 a"). Click (Function (event) {
- Block Default Events
- Event.preventdefault ();//page will not jump, but will print out 1,
- });
- $ (". Box1"). Click (function () {
- Console.log ("1");
- });
Fourth type
[HTML]View PlainCopyprint?
- $ (". Box1"). Click (function () {
- Console.log ("1")
- });
- Stop bubbling
- $ (". Box1 a"). Click (Function (event) {
- Event.stoppropagation ();
- Block Default Events
- Event.preventdefault ()//page does not jump and will not print out 1
- })
Fifth type
[HTML]View PlainCopyprint?
- $ (". Box1"). Click (function () {
- console.log ("1")
- &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;});
- $ (". Box1 a") . Click (Function (event) {
- The return false; //page does not jump, nor does it print 1, equals both event.stoppropagation () and Event.preventdefault () are called;
-
-
Developer test-jquery-block bubbling