我的 jQuery 有问题click
当选择器是 a 时div
那是在另一个之上div
正如这里所看到的..
the html
<div id="parent">
<div id="child">
</div>
</div>
css
#parent{
background-color:red;
width:100px;
height:100px;
position:relative;
}
#parent:hover #child {
display:block;
}
#child{
background-color:yellow;
width:10px;
height:10px;
border: 1px solid black;
position:absolute;
display:none;
bottom:0;
right:0;
}
js
$("#parent").bind("click", function() {
alert('you clicked on red');
});
$("#child").bind("click", function() {
alert('you click on yellow');
});
如果你点击红色#parent
它会发出适当的警报。如果你点击黄色#child
,它会发出黄色警报和红色警报。
See the jsfiddle http://jsfiddle.net/qTdkt/3/
有没有办法来解决这个问题?
使用 jQuery 的.stopPropagation() http://api.jquery.com/event.stopPropagation/
$("#parent").bind("click", function() {
alert('you clicked on red');
});
$("#child").bind("click", function(e) {
e.stopPropagation();
alert('you click on yellow');
});
http://jsfiddle.net/qTdkt/4/ http://jsfiddle.net/qTdkt/4/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)