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
PHPzOriginal
2016-05-16 17:03:241517browse

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

If e388a4556c0f65e1904146cc1a846bee is inside e388a4556c0f65e1904146cc1a846bee, then what? , 64e5601d0a941f4972a2954192bdae18 has an onclick event, e388a4556c0f65e1904146cc1a846bee also has an onclick event. In order to trigger the click event of 64e5601d0a941f4972a2954192bdae18 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;
  }
 }

6a2eef99229855cc57c99781d198d750 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:

function stopDefault( e ) {
         //阻止默认浏览器动作(W3C)
         if ( e && e.preventDefault )
             e.preventDefault();
         //IE中阻止函数器默认动作的方式
         else
             window.event.returnValue = false;
         return false;
     }

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

Statement:
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