event-propagation

Event propagation in Javascript

最后都变了- 提交于 2019-11-27 04:26:19
If I have an element (html) nested in another element and both of them have a click handler attached, clicking the inner element executes its click handler and then bubbles up to the parent and executes its click handler. That's how I understand it. Do events bubble up the DOM tree if there are no events attached that are the same and if so, is it worth putting a event.stopPropagation() at the end of every handler to stop this and speed things up? events almost always bubble up unless event.cancelBubble=true is set or event.stopPropagation() is used. You are only aware of it, though, when one

React prevent event bubbling in nested components on click

与世无争的帅哥 提交于 2019-11-27 03:22:32
Here's a basic component. Both the <ul> and <li> have onClick functions. I want only the onClick on the <li> to fire, not the <ul> . How can I achieve this? I've played around with e.preventDefault(), e.stopPropagation(), to no avail. class List extends React.Component { constructor(props) { super(props); } handleClick() { // do something } render() { return ( <ul onClick={(e) => { console.log('parent'); this.handleClick(); }} > <li onClick={(e) => { console.log('child'); // prevent default? prevent propagation? this.handleClick(); }} > </li> </ul> ) } } // => parent // => child Will Stone I

Javascript : How to enable stopPropagation?

99封情书 提交于 2019-11-26 20:00:46
问题 With object.stopPropagation() I can stop event bubbling but how can I re-enable it? Is there a pre defined function in js something like object.startPropagation ? EDIT: The Problem is that the JS remembers if you click on the "object" than stop Event Bubbling always even after I don't want it, so I would like to stop it: document.getElementById("object").onclick = function(e){ if(e && e.stopPropagation) { e.stopPropagation(); } else { e = window.event; e.cancelBubble = true; } } 回答1: It doesn

Stop mouse event propagation

孤街浪徒 提交于 2019-11-26 12:45:46
What is the easiest way to stop mouse events propagation in Angular 2? Should I pass special $event object and call stopPropagation() myself or there is some other way. For example in Meteor I can simply return false from event handler. If you want to be able to add this to any elements without having to copy/paste the same code over and over again, you can make a directive to do this. It is as simple as below: import {Directive, HostListener} from "@angular/core"; @Directive({ selector: "[click-stop-propagation]" }) export class ClickStopPropagation { @HostListener("click", ["$event"]) public

Event propagation in Javascript

别说谁变了你拦得住时间么 提交于 2019-11-26 11:11:45
问题 If I have an element (html) nested in another element and both of them have a click handler attached, clicking the inner element executes its click handler and then bubbles up to the parent and executes its click handler. That\'s how I understand it. Do events bubble up the DOM tree if there are no events attached that are the same and if so, is it worth putting a event.stopPropagation() at the end of every handler to stop this and speed things up? 回答1: events almost always bubble up unless

Prevent scrolling of parent element when inner element scroll position reaches top/bottom? [duplicate]

孤街醉人 提交于 2019-11-26 03:17:03
问题 This question already has answers here : prevent Scroll bubbling from element to window (15 answers) Closed 10 months ago . I have a little \"floating tool box\" - a div with position:fixed; overflow:auto . Works just fine. But when scrolling inside that box (with the mouse wheel) and reaching the bottom OR top, the parent element \"takes over\" the \"scroll request\" : The document behind the tool box scrolls. - Which is annoying and not what the user \"asked for\". I\'m using jQuery and

Stop mouse event propagation

早过忘川 提交于 2019-11-26 02:42:11
问题 What is the easiest way to stop mouse events propagation in Angular 2? Should I pass special $event object and call stopPropagation() myself or there is some other way. For example in Meteor I can simply return false from event handler. 回答1: If you want to be able to add this to any elements without having to copy/paste the same code over and over again, you can make a directive to do this. It is as simple as below: import {Directive, HostListener} from "@angular/core"; @Directive({ selector:

How do I prevent a parent&#39;s onclick event from firing when a child anchor is clicked?

*爱你&永不变心* 提交于 2019-11-25 22:04:54
问题 I\'m currently using jQuery to make a div clickable and in this div I also have anchors. The problem I\'m running into is that when I click on an anchor both click events are firing (for the div and the anchor). How do I prevent the div\'s onclick event from firing when an anchor is clicked? Here\'s the broken code: JavaScript var url = $(\"#clickable a\").attr(\"href\"); $(\"#clickable\").click(function() { window.location = url; return true; }) HTML <div id=\"clickable\"> <!-- Other content

event.preventDefault() vs. return false

你说的曾经没有我的故事 提交于 2019-11-25 21:32:35
问题 When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I\'ll use jQuery in the examples, but this applies to plain-JS as well: 1. event.preventDefault() $(\'a\').click(function (e) { // custom handling here e.preventDefault(); }); 2. return false $(\'a\').click(function () { // custom handling here return false; }); Is there any significant difference between those two methods of stopping event propagation? For me, return