如果我有一个绝对定位的父母div,然后是一个具有较高Z-索引并相对定位的子div,是否有办法只有一个点击事件寄存器如果父div被点击,但不是内部div?对父母点击触发事件但不对孩子触发
Updated fiddle with text input example
如果我有一个绝对定位的父母div,然后是一个具有较高Z-索引并相对定位的子div,是否有办法只有一个点击事件寄存器如果父div被点击,但不是内部div?对父母点击触发事件但不对孩子触发
Updated fiddle with text input example
$(".parent").click(function(e) {
if (e.target == this) {
$(this).hide();
}
});
更改为:
$('.child a').click(function(e) {
$(this).parent('.child').hide();
});
,只有当孩子点击的作品。如果父母被点击会发生什么? – j08691
尝试这个
$('#child').click(function(event) {
event.stopPropagation();
alert('You clicked Child');
});
$('#parent').click(function() {
alert('You clicked on Parent');
});
您可以检查在这里工作 http://jsfiddle.net/VnHGh/24/
啊!非常简单,谢谢。 –
@ChristianBenincasa欢迎您:) – VisioN
http://stackoverflow.com/a/11124083/601179 – gdoron