Detect click outside div using javascript

后端 未结 7 1906
無奈伤痛
無奈伤痛 2020-12-02 13:10

I\'d like to detect a click inside or outside a div area. The tricky part is that the div will contain other elements and if one of the elements inside the div is clicked, i

7条回答
  •  天涯浪人
    2020-12-02 13:44

    In Angular 6 and IONIC 3, I do same as here:

    import {Component} from 'angular/core';
    
    @Component({
      selector: 'my-app',
      template: `
        
          
    ` }) export class AppComponent { onClick(event) { var target = event.target || event.srcElement || event.currentTarget; if (document.getElementById('warning-container').contains(target)){ // Clicked in box } else{ // Clicked outside the box } } }

    This working fine on web/android/ios. It might be helpful for someone, Thanks.

提交回复
热议问题