如何停止将事件从父div传播到子div

问题描述 投票:4回答:4

我想在我的html中停止从父级div传播到子div的事件。这是一个简单的代码,我正在尝试做什么:

<div>
  <div id='categoryList'>
    <div class='listed-category'>
      <span>some content</span>
      <a id='close'>x</a> //"a" is used to remove the div
    </div>
  </div>
</div>
<div id='dropdown'>
</div>

在上面的代码中,如果我点击<div id='categoryList'><div id='dropdown'>将按照下面的代码向上和向下滑动。

$(document).ready(function () {

    $('#categoryList').click(function (event) {
        event.preventDefault();
        event.stopPropagation();
        if ($('#dropdown').is(":visible")) {
            $('#dropdown').slideUp(400);
        }
        else {
            $('#dropdown').slideDown(400);
        }
    });
})

但是当我点击孩子<div class='listed-category'>时,上面的JavaScript执行并且<div id='dropdown'>上下滑动。怎么阻止这个?然而,我希望能够点击<a id='close'>去除孩子div

jquery html5 dom
4个回答
10
投票

检查匹配点击对象的目标。在click事件处理程序的开头尝试此代码。

if( event.target !== this ) {
       return;
}
//...do stuff.. 

3
投票

你需要停止在孩子,而不是父母传播,所以你应该写这个:

 $('.listed-category').click(function(event){
    event.stopPropagation();
});

如果你想点击a标签并停止传播,你还需要写这个:

 $('a#close').click(function(event){
    event.stopPropagation();
});

无论如何,我会建议Yuriy的答案。我只是想让你知道你应该把event.stopPropagation()放在孩子们身上(如果你不想让父母的事件运行),而不是父母。


1
投票

而不是使用e.stopPropagation,我建议你在div <div class='listed-category'>中使用unbind()方法,如此...

$('.listed-category').unbind('click');

这将允许停止.listed-category div的传播效果。方法e.stopPropagation()不起作用,因为它只适用于父元素而不适用于子元素。

你总是可以创建一个新函数并在之后分配给.listed-category div。你也可以在这个链接上查看unbind()方法的文档:http://api.jquery.com/unbind/

我希望有所帮助!


0
投票

http://codepen.io/rakeshcode/pen/KrZdNo

$(document).ready(function(){
  $(".arrow").click(function(){
  $(".row").slideToggle();
  
  });

$(".btn").click(function(event){
   event.stopPropagation();
  //$(".btn").attr("href","www.yahoo.com")
});
})
.btn {
  display: block;
  padding:15px;
  background-color:green;
  width:100px;
  margin-top:15px;
  text-decoration:none;
  margin-left:auto;
  margin-right:auto;
  
  
  
}
.arrow {
  cursor:pointer;
  text-align: center;
  padding:10px 0;
  font-size:15px;
  font-weight:bold;
  background-color:grey;
  margin-top:15px;
}
.container {
  max-width:600px;
  margin-left:auto;
  margin-right:auto; 
}
.close {
  height:75px;
  overflow:hidden;
  transition-duration: 0.3s;  
  transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}
.open {
  height:215px;
  overflow: hidden;
  transition-property:height;
  -moz-transition-duration: 0.3s;
    -webkit-transition-duration: 0.3s;
    -o-transition-duration: 0.3s;
    transition-duration: 0.3s;  
  transition-timing-function: ease-in;
}
.up {
  background-color:red; 
}
.down {
  background-color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="arrow"> Click here Click here<a class="btn" href="www.google.com" target="_blank">click here</a></div>
  <div class="wrap">
  <div class="row">The most effective plac elearning,’ they most likely don’t know this themselves.Well, the most effective place to start is to dig into what the person asking for this training, ACTUALLY needs. Because if they’re dumping a huge stack of content on your desk and giving you vague instructions to ‘turn it into elearning,’ they most likely don’t know this themselves.Well, the most effective place to start is to dig into what the person asking for this training.ACTUALLY needs. Because if they’re dumping a huge stack of content on your desk and giving you vague instructions to ‘turn it into elearning,’ they most likely don’t know this themselves.Well, the most effective place to start is to dig into what the person asking for this trainingACTUALLY needs. Because if they’re dumping a huge stack of content on your desk and giving you vague instructions to ‘turn it into elearning,’ they most likely don’t know this themselves.Well, the most effective place to start is to dig into what the person asking for this training</div>
  </div>
  </div>
© www.soinside.com 2019 - 2024. All rights reserved.