问题
I want to stop propagate event from parent div to child div in my html. Here a simple code what I'm trying to do:
<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>
In the above code if I click on <div id='categoryList'> the <div id='dropdown'> will slide-up and down by bellow code.
$(document).ready(function () {
$('#categoryList').click(function (event) {
event.preventDefault();
event.stopPropagation();
if ($('#dropdown').is(":visible")) {
$('#dropdown').slideUp(400);
}
else {
$('#dropdown').slideDown(400);
}
});
})
But when I click on the child <div class='listed-category'> the above JavaScript executes and the <div id='dropdown'> is sliding up and down. How to stop this? Yet I want to be able to click on <a id='close'> to remove the child div
回答1:
Check target for match clicked object. Try this code in beginning of click event handler.
if( event.target !== this ) {
return;
}
//...do stuff..
回答2:
you need to stop propagating in child,not parent so you should write this:
$('.listed-category').click(function(event){
event.stopPropagation();
});
and if you want to click a tag and stop propagation too,you also need to write this:
$('a#close').click(function(event){
event.stopPropagation();
});
Anyways I will suggest Yuriy's answer. I just want you to know that you should put event.stopPropagation() in childrens(if you don't want event of parent is run),instead of parent.
回答3:
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>
回答4:
Instead of using e.stopPropagation, I suggest you to use unbind() method in the div <div class='listed-category'> like so...
$('.listed-category').unbind('click');
This will allow to stop the propagation effect for the .listed-category div. The method e.stopPropagation() doesn't work because it just works with parent elements and not with child elements.
You can always create a new function and assign to the .listed-category div afterwards.. You can also check the documentation for the unbind() method at this link: http://api.jquery.com/unbind/
I hope that helps!
来源:https://stackoverflow.com/questions/28210108/how-to-stop-propagating-event-from-parent-div-to-child-div