Typescript 3 Angular 7 StopPropagation and PreventDefault not working

后端 未结 3 1392
独厮守ぢ
独厮守ぢ 2021-02-18 18:16

I have a text input inside a div. Clicking on the input should set it to focus and stop the bubbling of the div click event. I\'ve tried the stopPropagation and

3条回答
  •  醉梦人生
    2021-02-18 18:31

    You can only stop propagation for the same event.

    Your fireEvent function stops propagation for your mousedown event, but not for your click event.

    If you want to stop propagation to click, try to add another click event on the input and stop propagation from there

    For instance

    
    

    Your other function only needs to know what is required, that is, set focus

    fireEvent(e) {
        this.inputBox.nativeElement.focus();
        console.log('click inside input');
    }
    

    preventDefault() prevents the default behaviour, it is not related to bubbling or not the events, so you can safely ignore it

提交回复
热议问题