Home > Web Front-end > JS Tutorial > A simple way to prevent bubbling events and default events from happening in JS

A simple way to prevent bubbling events and default events from happening in JS

PHPz
Release: 2018-09-30 16:01:11
Original
1459 people have browsed it

This article mainly introduces the simple method of preventing bubbling events and default events in JS. Friends in need can refer to it

If

is inside

, then what? ,

has an onclick event,

also has an onclick event. In order to trigger the click event of

without triggering the click event of the parent element, you need to call the following function:

function stopBubble(e){
  if(e&&e.stopPropagation){//非IE
   e.stopPropagation();
  }
  else{//IE
   window.event.cancelBubble=true;
  }
 }
Copy after login

If you want to prevent the default event from being triggered, that is, the default href event, then you need to call the following function:

The above is the entire content of this chapter, more For more related tutorials, please visit JavaScript Video Tutorial!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template