HTML:
<ul>
<li>
<a href="http://wp.pl">wp.pl</a>
</li>
</ul>
Javascript:
$(document).ready(function() {
$body = $('body');
$body.on('click', 'a', function(event) {
event.preventDefault();
event.stopPropagation();
/**
* Causes the "This also..." never shows, but "Why is..." still appears
*/
//event.stopImmediatePropagation();
alert('Should be seen always');
});
$body.on('click', 'a', function(event) {
alert('This also should be seen - same level as "a"');
});
$body.on('click', 'li', function() {
alert('This never shows - and that is ok - because is under "a"');
});
$body.find('ul').on('click', 'li', function() {
alert('Why is this shown?');
});
});
在这里摆弄:http: //jsfiddle.net/g3PEr/1/
问题是:为什么即使在其子元素具有or的情况下,也会在click
被触发的元素上设置事件?find()
stopPropagation()
stopImmediatePropagation()
请阅读接受的答案及其评论以了解为什么会发生这种情况。