admin管理员组文章数量:1415467
How do I do something like:
<div onclick="foo1()">
<div onclick="foo2()"></div>
</div>
When I do that and I click the child element, it still runs the foo1() function. How do I temporarily disable the parent element or something?
How do I do something like:
<div onclick="foo1()">
<div onclick="foo2()"></div>
</div>
When I do that and I click the child element, it still runs the foo1() function. How do I temporarily disable the parent element or something?
Share Improve this question edited Feb 1, 2015 at 18:24 Deduplicator 45.8k7 gold badges72 silver badges123 bronze badges asked Aug 5, 2013 at 2:19 Thomas LaiThomas Lai 3171 gold badge6 silver badges16 bronze badges 04 Answers
Reset to default 3With jQuery, you can use this: http://api.jquery./event.stopPropagation/
I have created a working EXAMPLE for you
HTML
<div id="parent">
<div id="child"></div>
</div>
CSS (just to distinguish the two divs)
#parent {
background-color: black;
width: 220px;
height: 220px;
}
#child {
position:relative;
background-color: blue;
width: 110px;
height: 110px;
top:160px;
left:160px;
}
JavaScript ( include jQuery )
$(document).ready(function(){
$('#child').click(function() {
alert('Child');
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
});
$('#parent').click(function() {
alert('Parent');
});
});
When you click on the child ONLY the action from the child is getting actioned. You can modify my example as you like to achieve what you need.
Hope this works for you.
Yes, jQuery can solve your problem. See the code below:
<script src="jquery.js" type="text/javascript"></script>
<script>
function a()
{
alert('parent');
}
$(document).ready(function(){
$('#div2').click(function(e){alert('child');e.stopPropagation();})
})
</script>
<div onclick="a();" style="height:100px;width:100px;border:1px solid red">
<div id="div2" style="height:85px;width:85px;border:1px solid green">
</div>
</div>
You can try
<div onclick="foo1(event)">
parent
<div onclick="foo2(event)">child</div>
</div>
And
function foo1(e){
console.log('foo1', e)
}
function foo2(e){
e.stopPropagation();
console.log('foo2', e)
}
Demo: Fiddle
本文标签: javascripthave parent div clickable with clickable child divStack Overflow
版权声明:本文标题:javascript - have parent div clickable with clickable child div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745180797a2646455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论