我们正在做日志记录,我们有一个网站标题,只要单击该网站就会在其中进行记录。我们在元素中也有需要记录的元素。有没有一种方法可以仅通过使用jquery.not()
函数或其他任何方式在标题中单击时记录子元素?
的HTML
<div id="header">
<div><a class="box blue" href="#">Blue</a></div>
<div><a class="box red" href="#">Red</a></div>
<div><a class="box green" href="#">Green</a></div>
</div>
的CSS
$('#header').on('click', function()
alert('header')
});
$('.box').on('click', function() {
alert($(this).attr('class'));
});
您可以使用jQuery的.stopPropagation()
功能停止从子级到父级的冒泡:
$('#header').on('click', function() {
alert('header');
});
$('.box').on('click', function(e) {
e.stopPropagation();
alert($(this).attr('class'));
});
#header { padding:20px; background:yellow; overflow:auto; }
.box { width:50px; height:50px; float:left; margin:0 20px 0 0; }
.blue { background:blue; }
.red { background:red; }
.green { background:green; }
a { color:white; display:block; text-align:center; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="header">
<div><a class="box blue" href="#">Blue</a></div>
<div><a class="box red" href="#">Red</a></div>
<div><a class="box green" href="#">Green</a></div>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句