1. Event.preventDefault (); - The default event to prevent elements.
Note: The default event of the jump of the A element,
Button, RADIO and other format elements of the default events,
DIV element has no default event
example:
Copy code code as follows:
<a href = "http://www.baidu.com" target = "_ black"> Baidu </a>
Copy code code as follows:
var samp = document.Getelementbytagname ("a");
SAMP.AdDeventListener ("Click", Function (E) {e.preventdefault ()}, false);
Explanation: When you click on the link, jump will occur under normal circumstances, but now we prevent its default event, that is, the jumping incident, and then we will not jump to Baidu.
2. Event.stopPropagation (); - Prevent element bubbling incident
Note: The nested elements generally have bubbling incidents, which will bring some effects
example:
Copy code code as follows:
<div ID = "c1" onClick = "Alert (1)">
<div ID = "c2" online = "alert (2)">
<input type = "Button" id = "c3" value = "click" OnClick = "Alert (3)">
</div>
</div>
When you click Button here, the browser will pop up 3,2,1. Originally, I only wanted to let the incidents binding on Button happened, but I accidentally triggered the two parent -level events. Here we just did it here. A simple test, imagine that if a button and his father -level are bound to an important incident at the same time in the project development, the results will be terrible. The processing method at this time is to prevent bubbling.
Register the click incident to the input while preventing its bubbling incident
Copy code code as follows:
document.GetelementByid ('C3'). AddeventListener ('Click', Function (E) {e.stoppRopagation ()}, false);
OK! Intersection Intersection Be
PS: Here I recommend an online query tool for the JS incident. It summarizes the types and function functions commonly used by JS:
JavaScript event and function description Daquan:
http://tools.vevb.com/table/javaScript_event