gpt4 book ai didi

jQuery 重复元素及其后面的数字

转载 作者:行者123 更新时间:2023-12-01 03:50:51 25 4
gpt4 key购买 nike

如何在这里使用更少的代码?我不想对每个元素重复相同的步骤。

$('.slider1').stop().hover(function () {
$('#slider_1').fadeIn('fast');
});
$('.slider1').stop().mouseleave(function () {
$('#slider_1').hide('fast');
});

$('.slider2').stop().hover(function () {
$('#slider_2').fadeIn('fast');
});
$('.slider2').stop().mouseleave(function () {
$('#slider_2').hide('fast');
});

$('.slider3').stop().hover(function () {
$('#slider_3').fadeIn('fast');
});
$('.slider3').stop().mouseleave(function () {
$('#slider_3').hide('fast');
});

最佳答案

试试这个

$.each([1,2,3,4], function(i, val){
$('.slider' + val).stop().hover(function(){
$('#slider_' + val).fadeIn('fast');
},
function(){
$('#slider_' + val).hide('fast');
});
});

关于jQuery 重复元素及其后面的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8807206/

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