javascript - 为什么我的 JavaScript 事件侦听器条件不起作用?
问题描述
当我单击文档中的任意位置时执行此事件侦听器,我希望在单击this.dropdown
或单击时不执行它this.inputField
。现在,即使我单击这两个元素,它也会执行。
constructor(inputField, monthInput, previousMonth, nextMonth, dates, dropdown, document) {
this.monthInput = monthInput;
this.previousMonth = previousMonth;
this.nextMonth = nextMonth;
this.dates = dates;
this.dropdown = dropdown;
this.inputField = inputField;
this.document = document;
this.document.addEventListener('click', (e) => {
if (e.target.id !== this.dropdown.id && e.target.id !== this.inputField.id) {
this.dropdown.style.display = 'none';
}
});
}
感谢您的任何建议!
解决方案
这解决了这个问题:
checkPathForId(path) {
for (let item of path) {
if (item === this.dropdown || item === this.inputField) {
return true;
}
}
return false;
}
我一直在定位错误的元素,我试图获取的元素是父元素。
推荐阅读
- python - 使用 Python/Tornado 时,是否可以通过处理程序中的 http 请求调用另一个 API?
- apache-spark - 在 JDBC 连接处带有空格的表名给出错误
- visual-studio - 为什么 Visual Studio 2019 让一切变得更大?
- terraform - Terraform 不使用提供的 AWS 配置文件?
- android - 如何实现 Room LiveData 过滤器
- reactjs - 在一个文件中反应 js 更多功能反应卸载
- dataframe - 计算 Yes 和 No 的 data.frame 中的单词频率
- typescript - SPFx ChartJS Web 部件
- amazon-web-services - AWS - 安全组报告 powershell
- sql - HiveQL:如何编写查询以根据嵌套的 JSON 数组值选择和过滤记录