javascript - 删除事件侦听器无法正常工作
问题描述
我只是尝试将事件监听器和删除事件监听器添加到元素,但它没有删除。
我想问题可能出在参数上,但我用它们来跟踪 DRY。所以我可以简单地重用它,比如 nextSection.addEventListener('mouseover', showContent(event, nextSection)) 等等,所以我不需要任何 if 语句或类似的东西。
* 编辑 *
我做了一些我将要使用的元素的例子。有机会,会有更多的事件。如果我不使用参数,就会有更多的功能。另外,移动设备上会有点击而不是鼠标事件,所以我需要删除它们。
据我所知,问题出在 return 语句上。如果我使用事件而不是参数等 event.target 我得到一些奇怪的错误。
const loginSection = document.querySelector('#js-login-section');
const searchSection = document.querySelector('#js-search-section');
const shoppingBagSection = document.querySelector('#js-shopping-bag-section');
const wishlistSection = document.querySelector('#js-wishlist-section');
function showContent(element) {
return () => {
const toggle = element.lastElementChild;
toggle.style.maxHeight = toggle.scrollHeight + 'px';
}
}
function hideContent(element) {
return () => {
const toggle = element.lastElementChild;
toggle.style.maxHeight = null;
}
}
/* Media queries - min width 992px */
loginSection.addEventListener('mouseover', showContent(loginSection));
loginSection.addEventListener('mouseout', hideContent(loginSection));
searchSection.addEventListener('mouseover', showContent(searchSection));
searchSection.addEventListener('mouseout', hideContent(searchSection));
shoppingBagSection.addEventListener('mouseover', showContent(shoppingBagSection));
shoppingBagSection.addEventListener('mouseout', hideContent(shoppingBagSection));
wishlistSection.addEventListener('mouseover', showContent(wishlistSection));
wishlistSection.addEventListener('mouseout', hideContent(wishlistSection));
/* Media queries - max width 992px */
loginSection.removeEventListener('mouseover', showContent(loginSection));
loginSection.removeEventListener('mouseout', hideContent(loginSection));
searchSection.removeEventListener('mouseover', showContent(searchSection));
searchSection.removeEventListener('mouseout', hideContent(searchSection));
shoppingBagSection.removeEventListener('mouseover', showContent(shoppingBagSection));
shoppingBagSection.removeEventListener('mouseout', hideContent(shoppingBagSection));
wishlistSection.removeEventListener('mouseover', showContent(wishlistSection));
wishlistSection.removeEventListener('mouseout', hideContent(wishlistSection));
先感谢您!
解决方案
发生的事情是return () => {};
每次运行时都会返回一个新函数。因此,每次您调用其中一个函数时,都会创建一个新的事件处理程序。
这意味着添加的处理程序与您要删除的处理程序不同。
为了解决这个问题,我会保持简单:
const loginSection = document.querySelector('#js-login-section');
function showContent(e)
{
const toggle = e.currentTarget.lastElementChild;
toggle.style.maxHeight = toggle.scrollHeight + 'px';
}
function hideContent(e)
{
const toggle = e.currentTarget.lastElementChild;
toggle.style.maxHeight = null;
}
loginSection.addEventListener('mouseover', showContent);
loginSection.addEventListener('mouseout', hideContent);
loginSection.removeEventListener('mouseover', showContent);
loginSection.removeEventListener('mouseout', hideContent);
我不确定你想避免重复什么,所以我不能就此提出建议,但我相信你会弄清楚的。
推荐阅读
- google-apps-script - ScriptApp.getService().getUrl() 指向开发 URL。我怎样才能让它指向 exec 生产 URL?
- sql - Mariadb 扫描时间戳列中的所有分区
- asp.net-core - 如何在 ASP.NET Core 3.1 中禁用 JSON OK 响应(但不是错误)
- python - 递增时间戳
- javascript - onClick 没有触发,事件是一个函数
- flyway - flyway 是否支持基于身份验证密钥的 Snowflake 凭据
- cognos-11 - 从提示值中排除/限制到当前年份 - Cogno BI 11
- wordpress - Woocommerce:订单商品未显示在订单电子邮件中
- android - SweepGradient Android - 如何设置渐变的起始角度
- java - 当我使用 printf 时,它给了我“???”