Blocking bubbling Event resolution 2

Source: Internet
Author: User

In the event of a click, determine whether the object (E.target) is a parent element (which can be judged to have a unique class for the parent element), and if so, the first-level click event.

Give me a chestnut:

Html:

<DivID= "ClickMe">Click Me</Div><DivID= "BG"class= "BG">  <DivID= "inner">    <H2>Content Area</H2>    <DivID= "BTN">Click Me</Div>  </Div></Div>

Css:

body {  width:100%;  Position:absolute;  left:0;  top:0;  bottom:0} #bg {  width:100%;  Position:absolute;  left:0;  top:0;  bottom:0;  Background:rgba (0, 0, 0,. 5);  Display:none;} #bg. Show {  display:block;} #inner {  width:200px;  height:200px;  Background: #fff;}

Js:

var function (elem) {    return  document.getElementById (elem)}$ (function() {    $ ( ' BG '). Classlist.add (' show ')}$ (function(e) {    if( E.target.classlist.contains (' bg ')) {      this. Classlist.remove (' Show')  }}$ (  function() {    $ (' BG '). Classlist.remove (' Show ')}

Execution: https://jsfiddle.net/9u4y1sru/

Jquery:

Html:

<Pclass= "BTN">Click Me</P><Divclass= "BG">      <ahref= "javascript:;"class= "BTN2">Click Me2</a></Div>

Css:

. BG{position:fixed;width:100%;Height:100%;background:Rgba (0,0,0,0.3);Display:None;Top:0; Left:0;}. BG. btn2{Display:Block;width:200px;Height:200px;background:#fff;margin:100px;padding:200px;}

Js:

$ ('. btn '). Click (function(e) {            $ ('. BG '). Show ();          });          $ ('. BG '). Click (function(e) {            if($ (e.target). Hasclass (' BG ')) {              $ ( '. BG '). Hide ();            }          );          $ ('. btn2 '). Click (function() {            alert (' I'm still in ');          });

Blocking bubbling Event resolution 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.