[英]Activate click only when not clicking on child element as well in javascript
[英]How to not activate mother when clicking child?
http://jsfiddle.net/aQb9H/這樣做的最佳方法是什么,請不要在單擊其子級時激活其父級。 現在,在單擊孩子時,兩個都被激活。
<!DOCTYPE html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>
$(document).ready(function(){
$('#child').click(function(){
$('#output').append('Child is activated ||')
})
$('#mother').on('mousedown',function(){
$('#output').append('Mother is activated ||')
})
})
</script>
</head>
<style>
#mother,#child,#output{
display:block;
color:white;
font-weight:bold;
cursor:pointer;
}
#mother{
width:100px;height:100px;
padding:30px;
margin:20px;
background-color:purple;
}
#child{
width:100px;height:100px;
background-color:orange;
}
#output{
width:300px;height:100px;
border:2px solid red;
color:black;
}
</style>
<body>
<h1>Click Child, I don't want to activate mother when clicking child</h1>
<div id='mother'>
Mother
<div id='child'>Child</div>
</div>
<div id='output'> Output: </div>
</body>
</html>
嘗試這個
$('#child').on('mousedown', function(e){
e.stopPropagation();
$('#output').append('Child is activated ||')
})
$('#mother').on('mousedown',function(){
$('#output').append('Mother is activated ||')
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.