我正在学习jQuery的基础知识,当我发现鼠标离开和鼠标输入动作时,我开始想知道我应该把鼠标离开动作放在哪里?Which1更正确,并且总是可以工作吗?
$(document).ready(function(){
$('div').mouseenter(function(){
$(this).fadeTo('fast','1');
$(this).mouseleave(function(){
$(this).fadeTo('fast','0.25');
});
});
});或者也许this1更好?
$(document).ready(function(){
$('div').mouseenter(function(){
$(this).fadeTo('fast','1');
});
$('div').mouseleave(function(){
$(this).fadeTo('fast','0.25');
});
});发布于 2013-03-23 11:26:24
你的第二个选项更正确,它应该一直只有一个事件设置。您的第一个选项是在每次触发mouseenter时添加一个新的mouseleave事件,从而导致许多附加事件。所以使用:
$('div').mouseenter(function () {
$(this).fadeTo('fast', 'fast');
});
$('div').mouseleave(function () {
$(this).fadeTo('fast', '0.25');
});实际上,在.hover(handlerIn, handlerOut)中有一些很好的简写。
$('div').hover(
function () { $(this).fadeTo('fast', 'fast'); },
function () { $(this).fadeTo('fast', '0.25'); }
);https://stackoverflow.com/questions/15582992
复制相似问题