gpt4 book ai didi

jquery - Mouseleave 在 mouseenter 里面还是外面?

转载 作者:搜寻专家 更新时间:2023-10-31 08:34:17 24 4
gpt4 key购买 nike

我正在学习 jQuery 的基础知识,当我发现 mouseleave 和 mouseenter 操作时,我开始想知道我应该把 mouseleave 操作放在哪里?哪一个更正确并且总是有效?

$(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');
});
});

最佳答案

你的第二个选项更正确,它应该一直有一个单一的事件设置。您的第一个选项添加一个新的 mouseleave每次事件mouseenter被触发,导致许多附加事件。所以使用:

$('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'); }
);

关于jquery - Mouseleave 在 mouseenter 里面还是外面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15582992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com