Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击子项时不单击_Javascript_Jquery_Jquery Events - Fatal编程技术网

Javascript 单击子项时不单击

Javascript 单击子项时不单击,javascript,jquery,jquery-events,Javascript,Jquery,Jquery Events,我有以下html代码: <div class="outerElement"> <div class="text"> Lorem ipsum dolar sit amet </div> <div class="attachment"> <!-- Image from youtube video here --> </div> </div> 同侧盲侧盲侧盲侧盲

我有以下html代码:

<div class="outerElement">
    <div class="text">
     Lorem ipsum dolar sit amet
    </div>
    <div class="attachment">
      <!-- Image from youtube video here -->
    </div>
</div>

同侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲侧盲

我在
.outerement
上有一个jQuery onclick事件,但是,我不想在单击附件时调用
.outerement
onclick事件,有什么方法可以防止这种情况或检查单击了哪个元素吗?

在子元素上使用
event.stopPropagation()

$(".attachment").on("click", function(event){
  event.stopPropagation();
  console.log( "I was clicked, but my parent will not be." );
});
这可以防止事件将DOM冒泡到父节点

事件
对象的另一部分是。这将告诉您从哪个元素触发事件开始。然而,在这种情况下,
stopPropagation
似乎是最好的解决方案

$(".outerElement").on("click", function(event){
  console.log( event.target );
});

我不确定在单击
附件
项目时是否可以防止触发事件,但您当然可以对其进行筛选

$('.outerElement').click(function() {
  if ($(this).hasClass('attachment')) {
    return;
  }
  // Handle event
});

这在没有jQuery的情况下是有效的

<div class="outerElement">
    <div class="attachment">Hello</div>
</div>

<script type="text/javascript">
document.getElementsByClassName('outerElement').onclick = function(){

    alert('You clicked on parent');

}
document.getElementsByClassName('attachment').onclick = function(){

    event.stopPropagation();
    alert('You clicked on child');

}
</script>

你好
document.getElementsByClassName('outerement')。onclick=function(){
警报(“您单击了父项”);
}
document.getElementsByClassName('attachment')。onclick=function(){
event.stopPropagation();
警报(“你点击了孩子”);
}

只需在子元素上设置
onclick=“event.stopPropagation();”

我不清楚允许从子元素传播对性能的影响,但我通过比较event.target和event.currentTarget解决了这一问题:

onClick={(event) => {
  if (event.target === event.currentTarget) {
     console.log('Handle click');
  }
}}
这是反应^ 更通用的javascript代码是:

$('.outerElement').click(function(event) {
  if (event.currentTarget !== event.target) {
    return;
  }
  // Handle event
});

您还可以过滤掉特定的图元类型,如下所示:

$('.outerElement').click(function(event) {
  if (['input', 'select', 'option'].indexOf(event.target.localName) >= 0) {
    return;
  }
  // Handle event
});


和的可能重复。抱歉,我在google和stackoverflow中进行了一些搜索,但找不到任何对我有帮助的结果。