admin管理员组文章数量:1332345
I've got a close button in a DIV, with both the DIV and the close button also having click()
events attached to them. When I click the close button it also triggers the div click()
event. I know I could put a SPAN inside the div and attach my click event to the span instead of the DIV, but is there a way I can keep both the div click event and the close button click event and not trigger the div click()
when I click the close button?
<div id="sendFeedback" class="feedback with-round-borders with-padding with-box-shadows">Send us your feedback <span id="closeFeedback" class="ui-icon ui-icon-circle-close" style="float:right"></span></div>
<script>
$(document).ready(function() {
$("#sendFeedback").click(function() {
alert('test');
});
$("#closeFeedback").click(function() {
$("#sendFeedback").fadeOut();
})
});
</script>
/
I've got a close button in a DIV, with both the DIV and the close button also having click()
events attached to them. When I click the close button it also triggers the div click()
event. I know I could put a SPAN inside the div and attach my click event to the span instead of the DIV, but is there a way I can keep both the div click event and the close button click event and not trigger the div click()
when I click the close button?
<div id="sendFeedback" class="feedback with-round-borders with-padding with-box-shadows">Send us your feedback <span id="closeFeedback" class="ui-icon ui-icon-circle-close" style="float:right"></span></div>
<script>
$(document).ready(function() {
$("#sendFeedback").click(function() {
alert('test');
});
$("#closeFeedback").click(function() {
$("#sendFeedback").fadeOut();
})
});
</script>
http://jsfiddle/t5VPN/1/
Share Improve this question edited Dec 5, 2011 at 20:27 themerlinproject asked Dec 5, 2011 at 20:23 themerlinprojectthemerlinproject 3,5825 gold badges39 silver badges55 bronze badges 1- It sounds like the click even is bubbling unexpectedly. if you use jquery's $(selector).bind() method to attach the event, you can stop bubbling by declaring a value of false for the function's third argument. (i.e: $(selector).bind('click', function(){}, false).) – Aaron Commented Dec 5, 2011 at 20:28
3 Answers
Reset to default 6Add event.stopPropagation()
to the <span>
click event handler:
$(document).ready(function() {
$("#sendFeedback").click(function() {
alert('test');
});
$("#closeFeedback").click(function(event) {
event.stopPropagation();
$("#sendFeedback").fadeOut();
})
});
.stopPropagation()
will stop the event from bubbling up to the div (or any other ancestor): http://api.jquery./event.stopPropagation/
Here is your jsfiddle updated: http://jsfiddle/jasper/t5VPN/2/
Yep. You should cancel the bubbling of the event up in the DOM tree.
For futher info see the Event#stopPropagation() in the DOM Event documentation.
$('#closeFeedback').click(function(e){
$('#sendFeedback').fadeOut();
e.stopPropagation();
});
本文标签: javascriptClick close button on a div with triggering div click()Stack Overflow
版权声明:本文标题:javascript - Click close button on a div with triggering div click() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742325228a2453571.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论