简体   繁体   English

Click 事件同时被触发多次

[英]Click Event is fired multiple times at the same time

Within a click function, I have the below code:在点击功能中,我有以下代码:

home.addEventListener('click', () => {

       myAPP.mapview.map.layers.items.forEach(item => {
            item.visible = false;
        });
       //........... }

However, after the button is clicked once, it seems to continuously run/execute.然而,按钮被点击一次后,它似乎不断运行/执行。 How could I assure it executes, ie 'resets' just one time, just when the button or click function occurs?我怎么能保证它执行,即“重置”一次,就在按钮或点击功能发生时?

The problem you are having is that multiple dom elements are firing the same click event at the same time.您遇到的问题是多个 dom 元素同时触发同一个 click 事件。 You should have the following:您应该具备以下条件:

home.addEventListener('click', (e) => {
  // to prevent child/parent dom elements from firing the same event.
  if( e.target !== this ) {
    return;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM