gpt4 book ai didi

javascript - 编写这个 jQuery 函数的更好方法?

转载 作者:行者123 更新时间:2023-12-01 02:36:49 27 4
gpt4 key购买 nike

基本上,我试图削减一些代码,但我不知道该怎么做,我有 9 个 DIV 全部绝对定位在不同的位置。它们都是灰色的,但是当悬停时,悬停的 DIV 会淡出,相应的 DIV 也会淡入。有没有更好的写法?

$('#l1').hover(function () {
$(this).fadeOut('300');
$('#l1c').fadeIn('300')
});
$('#l2').hover(function () {
$(this).fadeOut('300');
$('#l2c').fadeIn('300')
});
$('#l3').hover(function () {
$(this).fadeOut('300');
$('#l3c').fadeIn('300')
});
$('#l4').hover(function () {
$(this).fadeOut('300');
$('#l4c').fadeIn('300')
});
$('#l5').hover(function () {
$(this).fadeOut('300');
$('#l5c').fadeIn('300')
});
$('#l6').hover(function () {
$(this).fadeOut('300');
$('#l6c').fadeIn('300')
});
$('#l7').hover(function () {
$(this).fadeOut('300');
$('#l7c').fadeIn('300')
});
$('#l7').hover(function () {
$(this).fadeOut('300');
$('#l7c').fadeIn('300')
});
$('#l1c').mouseout(function () {
$(this).fadeOut('300');
$('#l1').fadeIn('300')
});
$('#l2c').mouseout(function () {
$(this).fadeOut('300');
$('#l2').fadeIn('300')
});
$('#l3c').mouseout(function () {
$(this).fadeOut('300');
$('#l3').fadeIn('300')
});
$('#l4c').mouseout(function () {
$(this).fadeOut('300');
$('#l4').fadeIn('300')
});
$('#l5c').mouseout(function () {
$(this).fadeOut('300');
$('#l5').fadeIn('300')
});
$('#l6c').mouseout(function () {
$(this).fadeOut('300');
$('#l6').fadeIn('300')
});
$('#l7c').mouseout(function () {
$(this).fadeOut('300');
$('#l7').fadeIn('300')
});

最佳答案

$('#l1,#l2,#l3,#l4,#l5,#l6,#l7').hover(function() {
$(this).fadeOut(300);
$("#" + this.id + "c").fadeIn(300);
});

$('#l1c,#l2c,#l3c,#l4c,#l5c,#l6c,#l7c').mouseout(function() {
$(this).fadeOut(300);
$("#" + this.id.substring(0, this.id.length - 1)).fadeIn(300);
});

关于javascript - 编写这个 jQuery 函数的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4497049/

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