我正在尝试清理我的代码。只是想知道是否有办法更好地编写这段代码?我在下面添加了一段代码:
$('.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');
});
答案 0 :(得分:3)
简单来说,您可以使用.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');
});
来自docs:
一个或多个以空格分隔的事件类型 和可选命名空间,例如
"click"
或"keydown.myPlugin"
。