Closed budarin closed 7 years ago
hi, thanks for comments, will investigate it.
by the way, for cancelling event you can use .stopPropagation()
method in Event object.
$('.el').on('click', function(e) {
e.stopPropagation();
});
I have used it but without success
I think it's can happen when you add listeners to the same element.
In this case element the same, and stopPropagation
doesn't work: http://jsfiddle.net/u3eL4sa5/1/
In this case elements are different, and stopPropagation
works: http://jsfiddle.net/kjnudber/2/
And it's normal browser behaviour. But really, for greater compatibility with jQuery, I think I can override stopPropagation
method.
@budarin by the way, when you using events
in Backbone.View you have one element, like with http://jsfiddle.net/u3eL4sa5/1/.
I added event handlers both on span and link and tried to prevent run eventhandler on span
If you still have the problem, maybe event handler was added on the same element (for example Backbone do it in View for performance optimisations). In this case you can use e.stopImmediatePropagation()
.
Example 1
$('body').on('click', 'span', function(e) {
e.stopPropagation();
console.log('click');
});
$('body').on('click', 'span', function(e) {
console.log('click');
});
$('span').trigger('click');
// result:
// click
// click
Example 2
$('body').on('click', 'span', function(e) {
e.stopImmediatePropagation();
console.log('click');
});
$('body').on('click', 'span', function(e) {
console.log('click');
});
$('span').trigger('click');
// result:
// click
there need to cancel future event bubbling with "return false" like in jQuery. I'm not able to cancel event from futher propagation and bubbling ( how to?