首页 > 解决方案 > 父元素onclick中的子元素onclick

问题描述

我在“div标签”中有一个“支票簿”,“div标签”已经注册了一个onclick事件,当我点击复选框时,它会调用“div标签”的事件,如何捕获事件“支票簿”,我为支票簿尝试了“z-index”,但没有奏效

标签: javascripthtmlcsscheckbox

解决方案


试试这个将解决我添加的问题if (e.target !== this)

$('.comment_item').on('click', function(e) {
  if (e.target !== this)
    return;
  
  alert( 'comment_item' );
});

$('.check-item').on('click', function(e) {
  if (e.target !== this)
    return;
  
  alert( 'check-item' );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="comment_item">
    <input type="checkbox" class="check-item">
</div>


推荐阅读