kupriyanenko / jbone

JavaScript Library for Events and DOM manipulation. Replaces jQuery for Backbone (2.5kb gzipped)
http://jbone.js.org
MIT License
279 stars 35 forks source link

preventDefault in eventHandlers like in jQuery with "return false" #28

Closed budarin closed 7 years ago

budarin commented 10 years ago

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?

kupriyanenko commented 10 years ago

hi, thanks for comments, will investigate it.

kupriyanenko commented 10 years ago

by the way, for cancelling event you can use .stopPropagation() method in Event object.

$('.el').on('click', function(e) {
    e.stopPropagation();
});
budarin commented 10 years ago

I have used it but without success

kupriyanenko commented 10 years ago

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.

kupriyanenko commented 10 years ago

@budarin by the way, when you using events in Backbone.View you have one element, like with http://jsfiddle.net/u3eL4sa5/1/.

budarin commented 10 years ago

I added event handlers both on span and link and tried to prevent run eventhandler on span

kupriyanenko commented 9 years ago

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