简体   繁体   English

如何使用Jquery检测到内部div的click事件

[英]How to detect a click event to an inner div using Jquery

Im creating a menu for mobile, and I want to add a function for overlay click. 我正在为移动设备创建菜单,我想添加一个点击重叠功能。

When I click on menu (purple part), it doesn't need to close, but when I click on blue section, then its need to close. 当我单击菜单(紫色部分)时,它不需要关闭,但是当我单击蓝色部分时,则需要关闭它。

I wrote a jQuery, who gets only purple section, but when I click on blue part the alert didn't appear. 我写了一个jQuery,只得到紫色部分,但是当我单击蓝色部分时, alert没有出现。 There's gonna be my JSFiddle for test, to see. 将会有我的JSFiddle进行测试。

And here is my code 这是我的代码

 $('.outer-content .inner-content').on('click', function() { $(".outer-content .inner-content").data('clicked', 'yes'); var isClicked = $('.outer-content').data('clicked'); if (isClicked == 'yes') { alert("clicked the blue block"); } else { alert("clicked the purple block"); } }); 
 .outer-content { width: 100%; height: 200px; background: lightblue; position: relative; } .inner-content { width: 300px; background: purple; position: absolute; margin: auto; top: 0; bottom: 0; right: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="outer-content"> <div class="inner-content"></div> </div> 

Basically there are two event models in javascript. 基本上,javascript中有两个事件模型。 Event capturing and Event bubbling. 事件捕获和事件冒泡。 In event bubbling, if you click on inside div, the inside div click event fired first and then the outer div click fired. 在事件冒泡的情况下,如果单击内部div,则首先触发内部div点击事件,然后触发外部div。 while in event capturing, first the outer div event fired and than the inner div event fired. 在事件捕获中,首先触发外部div事件,然后触发内部div事件。 To stop event propagation, use this code in your click method. 要停止事件传播,请在您的click方法中使用此代码。

  e.stopPropagation();

JSFIDDLE JSFIDDLE

Your code: 您的代码:

 $('.outer-content').on('click', function(e) { alert("clicked the blue block"); }); $('.inner-content').on('click', function(e) { alert("clicked the purple block"); e.stopPropagation(); }); 
 .outer-content { width: 100%; height: 200px; background: lightblue; position: relative; } .inner-content { width: 300px; background: purple; position: absolute; margin: auto; top: 0; bottom: 0; right: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="outer-content"> <div class="inner-content"></div> </div> 

 $('.outer-content').on('click', function(event) { if ($(event.target).hasClass('inner-content')) { alert("clicked the purple block"); } else { alert("clicked the blue block"); } }); 
 .outer-content { width: 100%; height: 200px; background: lightblue; position: relative; } .inner-content { width: 300px; background: purple; position: absolute; margin: auto; top: 0; bottom: 0; right: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="outer-content"> <div class="inner-content"></div> </div> 

You can do that by adding event on outer content and then use the event.target property to check the element that has been clicked 您可以通过在outer content上添加事件,然后使用event.target属性来检查已单击的元素来做到这一点

$('.outer-content').on('click', function(e) {

  if( $(e.target).hasClass('outer-content')){
        alert("clicked the blue block");
  } else {
        alert("clicked the purple block");
  }
});

Due to event bubbling any event in the child element will be propagated to parent element as well. 由于事件冒泡,子元素中的任何事件也会传播到父元素。

 $('.outer-content, div:not("inner-content")').on('click', function() { $(".inner-content").slideToggle(); }); 
 .outer-content { width: 100%; height: 200px; background: lightblue; position: relative; } .inner-content { width: 300px; background: purple; position: absolute; margin: auto; top: 0; bottom: 0; right: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="outer-content"> <div class="inner-content"></div> </div> 

 $('div').on('click', function(e) { e.preventDefault(); e.stopImmediatePropagation() if($(e.target).is('.inner-content')){ alert("clicked the purple block"); }else{ alert("clicked the blue block"); } }); 
 .outer-content{ width:100%; height:200px; background:lightblue; position:relative; } .inner-content{ width:300px; background:purple; position:absolute; margin:auto; top:0; bottom:0; right:0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="outer-content"> <div class="inner-content"></div> </div> 

When I click on menu(purple part), it doesnt need to close, but when I click on blue section, then its need to close 当我单击菜单(紫色部分)时,它不需要关闭,但是当我单击蓝色部分时,则它需要关闭

Why not target the blue element directly and only process any code to close when it is clicked as seen below. 为什么不直接将蓝色元素作为目标,并且单击该蓝色元素时仅处理关闭任何代码,如下所示。

If you need other code to execute when the purple element is clicked, bind to that separately. 如果单击紫色元素时需要执行其他代码,请分别绑定到该代码。

 $('.outer-content').on('click', function(e) { if (e.target == this) { // add "close" code here alert("will close"); } }); // You still can add code when clicking the purple element if needed... $('.inner-content').on('click', function(e) { alert("I'm purple but separate code and will not close"); }); 
 .outer-content { width: 100%; height: 200px; background: lightblue; position: relative; } .inner-content { width: 300px; background: purple; position: absolute; margin: auto; top: 0; bottom: 0; right: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="outer-content"> <div class="inner-content"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM