简体   繁体   English

单击内部元素时如何停止外部div事件的触发器? 并且所有事件都绑定到$(document.body)

[英]how can I stop the trigger of the outer div event when I click the inner element? And all the events are bound to $(document.body)

How can I stop the trigger of the outer div event when I click the inner element? 单击内部元素时,如何停止外部div事件的触发器? And all the events are bound to $(document.body),so i think i should not stop event stopPropagation. 而且所有事件都绑定到$(document.body),所以我认为我不应该停止事件stopPropagation。 When I click $(".logLi"), I just want to trigger the inner ele event. 当我单击$(“。logLi”)时,我只想触发内部ele事件。 What is wrong with my code and how can i solve the problem? 我的代码有什么问题,我该如何解决? here is my code 这是我的代码

<div class="clomn">
  <div class="logLi">
   <span>aaaaaaa</span><em>bbbbbb</em>
 </div>
</div>
$(document.body).on("click", ".clomn .logLi", function(e) {
  console.log("inner");
})
$(document.body).on("click", ".clomn", function(e) {
  console.log("outer")
})

You can prevent the propagation of the event, but I prefer to check in the parent click handler to check whether it has occurred in a child element then discard it 您可以阻止事件的传播,但是我更喜欢检查父单击处理程序,以检查事件是否在子元素中发生,然后将其丢弃

 $(document.body).on("click", ".colmn .logLi", function(e) { console.log("inner: 2"); }) $(document.body).on("click", ".colmn", function(e) { if ($(e.target).closest('.colmn .logLi').length == 0) { console.log("outer"); } }) $(".colmn .logLi").click(function() { console.log('inner: 1') }) 
 .colmn { border: 1px solid red; padding: 15px; } .logLi { border: 1px solid green; padding: 15px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="colmn"> <div class="logLi"> <span>aaaaaaa</span><em>bbbbbb</em> </div> </div> 

find by parent class name of e.target ,if you clicked inside .logLi your parent class will be .logLi . 按父类名称查找e.target ,如果在.logLi单击,则父类将为.logLi Do not need two click functions . 不需要两个单击功能。

$(document.body).on("click", ".clomn .logLi", function(e) {
  if($(e.target).parent().attr("class") == "logLi"){
    console.log("inner");
  }
  else{
    console.log("outer");
  }
});

暂无
暂无

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

相关问题 如何使用 document.body 来 append 将放置在特定 DIV 中的表 - How can I use document.body to append a table that will be placed in a specific DIV 如何找到使用 Javascript 创建但不存在于 document.body 中的元素? - How can I locate an element which was created with Javascript and is not present in the document.body? 我何时应该在窗口与文档与document.body上观察Javascript事件? - When should I observe Javascript events on window vs. document vs. document.body? 为什么我需要使用Mootools Element方法扩展document.body $(document.body)? - Why do I need to do $(document.body) to extend document.body with Mootools Element methods? 如何将事件冒泡到文档而不是文档。 - How can an event bubble to document but not to document.body? 如何使用Jquery使内部div触发对外部div的拖动效果? - How can I make an inner div trigger a drag effect on an outer div using Jquery? 为什么我不能删除document.body属性? - Why can't I delete the document.body property? 我无法将 PreactX 组件直接渲染到 document.body 中 - I can't render PreactX component directly into document.body iPhone Safari游戏中的奇怪点击事件泡泡,它在冒泡到document.body之前停止 - Strange Click Event Bubble on iPhone safari that it stop before bubbling to document.body 如何在使用对象加载页面时在 js 中检查(document.body)? - how can i inspect(document.body) in js while loading the page using object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM