hammer.js: stopPropagation() not working with tap event
I can’t seem to get stopPropagation to work. I want to make the child div trigger the alert without causing the link to be called. I tried putting stopPropagation() and preventDefault() on all the events - I tried this in chrome and IOS.
<a href='http://www.google.com' target="_blank">boo
<div id='test_el' style='background:red;color:blue'>Hello world</div>
</a>
var element = document.getElementById('test_el');
var hammertime = Hammer(element,{
prevent_default:true
}).on("tap", function(event) {
event.stopPropagation();
event.preventDefault();
event.gesture.stopPropagation();
event.gesture.preventDefault();
event.gesture.srcEvent.stopPropagation();
event.gesture.srcEvent.preventDefault();
event.gesture.startEvent.stopPropagation();
event.gesture.startEvent.preventDefault();
alert('hello!');
});
Here is plunker
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 18 (1 by maintainers)
var element = document.getElementById(‘test_el’); var hammertime = Hammer(element).on(‘tap’, function(event) { element.parentElement.href = null; alert(‘hello!’); });
// Ftfy
You’re right with 1.
With 2. I disagree. My take:
thisworks in JavaScriptI haven’t seen anyone recommend
el.onclickoverel.addEventListenerfor years, and it seems all the libraries are usingaddEventListener(orattachEventas a fallback) anyways. Do you have any references?stopPropagation just stops the event from bubbling, not the other events from firing. Like when you call stopPropagation on a mouseover event, it doesnt stops mouseout or something. Preventing a link from getting clicked you can only do this with calling preventDefault on the Click event. (correct me if im wrong)
the ev.preventDefault() doesnt make any sense actually in Hammer.js, since the browsers dont have a tap event implemented, but it is created by the domEvents that hammer uses. The ev.gesture.preventDefault() makes more sense, it calls the mouse or touch event that hammer used for the detection. This could be touchstart or mousemove etc.