【发布时间】:2020-01-31 15:33:28
【问题描述】:
我有这段代码,它可以正常工作,但有时会卡住(点击时有时不会更改类)。为什么? JSFiddle
$('#button').hover(function(e) {
$('.box').css('animation', 'fade-in .3s forwards');
$(this).css('background-color', 'blue');
$(this).click(function() {
if ($(this).hasClass('on')) {
$(this).removeClass('on');
} else {
$('#button').removeClass();
$(this).addClass('on');
}
});
}, function() {
if ($(this).hasClass('on')) {
return;
} else {
$('.box').css('animation', 'fade-out .3s forwards');
$(this).css('background-color', 'red');
};
})
【问题讨论】:
-
您正在为每个
mouseenter事件添加一个新的click处理程序(=.hover()的第一个函数)。这不可能……;) -
旁注;这是很多非干的
$(this)电话。