使用具有相同功能的多个事件
using multiple events with the same function
我正在尝试清理我的代码。只是想知道是否有一种方法可以更好地编写这些代码?我在下面添加了一段代码:
$('.popUp-block').on("click",function() {
$('.protect-field-container').removeClass('is-displayed');
$(this).closest('.protect-field-container').addClass('is-displayed');
});
$('.popUp-block').hover(function() {
$('.protect-field-container').removeClass('is-displayed');
$(this).closest('.protect-field-container').addClass('is-displayed');
});
$('.popUp-block').focusout(function() {
$('.protect-field-container').removeClass('is-displayed');
});
$('.popUp-block').on("mouseout", function() {
$('.protect-field-container').removeClass('is-displayed');
});
用简单的方法,您可以使用.on()
:
$('.popUp-block').on("click mouseover",function() {
$('.protect-field-container').removeClass('is-displayed');
$(this).closest('.protect-field-container').addClass('is-displayed');
});
$('.popUp-block').on("mouseout focusout", function() {
$('.protect-field-container').removeClass('is-displayed');
});
来自文档:
一个或多个以空格分隔的事件类型和可选的命名空间,如
"click"
或"keydown.myPlugin"
相关文章:
- 添加使用现有功能的按键事件
- 使用具有相同功能的多个事件
- 使用ExtJS捕获功能键事件(F1-F12)
- 向现有事件侦听器添加功能
- 防止两个默认功能发生,并同时触发第三个事件
- 将事件处理程序放在HTML按钮上,而不覆盖其默认功能
- 如何在链接上附加onclick事件以使用javascript功能
- 运行多事件功能的更简洁的方式
- 项目点击和收听事件功能 煎茶触摸 2 列表
- 一个 PHP 文件中有两个事件功能
- 主干.js内部触发事件功能.它是如何优化的
- jQuery解绑点击事件功能,并根据情况重新附加
- 浏览器如何读取JavaScripts,它应该排在第一位(设置在顶部):事件?功能?或子函数
- iPhone谷歌图像搜索触摸事件功能是如何工作的
- jQuery UI可以'无法在事件功能之外获取所选选项卡
- jQuery UI事件功能扩展
- 设置并清除'点击'事件功能,不清除其他事件
- 发送额外的参数在谷歌地图事件功能
- 使用角度门架时,阻止事件/功能启动
- 为什么clearInterval不能在窗口模糊和焦点事件功能中工作