Example of JavaScript blocking event bubbling, javascript example
Previously, I encountered an event bubble problem and went to the Internet to search for it. Most of the results were the same code, and the post was not very smooth. When FF is involved, you can use e. stopPropagation ();. Somehow I did not succeed. However, I found that FF supports e. cancelBubble = true;, which is feasible after testing. Paste the code here, so you can find it everywhere. The compatibility of earlier versions of IE has not been tested yet. It can be improved later.
Copy codeThe Code is as follows:
// Cancel event bubbling
Function stopBubble (e ){
Var evt = (e )? E: window. event; // compatible with FF
Evt. cancelBubble = true; // evt. stopPropagation (); FF can be used to prevent bubbles.
};
In addition:
1. cancelBubble (html dom Event object attribute): If the Event handle wants to prevent the Event from spreading to an inclusive object, it must be set to true.
2. stopPropagation (html dom Event object method): terminates further propagation of an Event in the capture, target processing, or blister phase of the propagation process. After this method is called, the handler for processing the event on the node will be called, and the event will not be assigned to other nodes.
3. preventDefault (html dom Event object method) notifies the browser not to perform the default action associated with the Event.
Example:
Function stopBubble (e)
{
If (e & e. stopPropagation)
E. stopPropagation ()
Else
Window. event. cancelBubble = true
}
Put this stopBubble (e) function in the function that you want to stop event bubbles to prevent event bubbles.