简体   繁体   English

多个事件监听器

[英]Multiple event listeners

OK so i have the following questions:好的,所以我有以下问题:

Should i add an event listener directly to the parent element (modal popup box), where this element have 3 buttons (2 for closing the tab, 1 for submitting data).我应该将事件侦听器直接添加到parent element (模态弹出框),其中该元素有3 buttons (2 个用于关闭选项卡,1 个用于提交数据)。

And in the listener function to assign some class methods like so and manipulate the whole functionality (submit data and display UI):并在listener function中分配一些像这样的class methods并操纵整个功能(提交数据和显示 UI):

document.getElementById('weather-modal-container').addEventListener('click', modal);

function modal(e) {
  ui.closeModal(e);
  weather.submitData();
  e.stopPropagation();
}

Or should i add 2 click listeners to the parent element one for closing the modal and one for submitting the data.或者我应该向父元素添加 2 个click listeners ,一个用于关闭模式,另一个用于提交数据。

document.getElementById('weather-modal-container').addEventListener('click', closeModal);
document.getElementById('weather-modal-container').addEventListener('click', submitData);

Or one listener directly to the parent element to manipulate the UI and another listener for the button that's inside the container.或者一个直接监听parent element监听parent element来操作UI ,另一个监听器监听容器内的按钮。

document.getElementById('weather-modal-container').addEventListener('click', closeModal);
document.getElementById('button-inside-container').addEventListener('click', submitData);

Okay, and what about only 1 event listener for All ? 好吧,那么对于All来说只有1个事件侦听器呢?

 const MyDialog = document.querySelector('#weather-modal-container') MyDialog.onclick=e=>{ let role = e.target.dataset.role if (role) { e.preventDefault() console.clear() console.log (role) switch (role){ case 'submitData': console.log ('something is :', MyDialog.something.value) // stuff about submiting data break; case 'closeModal': // stuff about close modal break; } } } 
 #weather-modal-container { display: block; width: 30em; padding: 1em; border: 1px solid grey;} a[data-role] { color: red; float: right;} #weather-modal-container button { float: right; margin: 0 .3em 0 0 } 
 <form id="weather-modal-container"> <a href="#" title="closeModal" data-role="closeModal">X</a> <p>Bla bla bla bla</p> <input type="text" placeholder="with something" name="something" value="hello"> <a href="https://en.wikipedia.org" title="wikipedia" > wikipedia</a> <br> <button data-role="submitData">Send</button> <button data-role="closeModal">Cancel</button> </form> 

If you bind 2 click event handlers to the parent element only the last one will fire, meaning you will need to do additional checking in the function definition as to what you actually clicked. 如果将2个click事件处理程序绑定到父元素,则仅会触发最后一个事件处理程序,这意味着您将需要在函数定义中对实际单击的内容进行其他检查。

ie

if(event.target.id == 'button-inside-container') {
    // do something
}

so depending on the complexity of what you actually want, binding the event handler functions to the specific element may be less messy 因此,根据您实际想要的复杂性,将事件处理函数绑定到特定元素可能会少一些麻烦

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

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