我有一堆重复,我知道我可以提高效率,但是对于循环来说却没有做太多的事情。之前。
我已经开始了下面的循环,但是我甚至不确定我是否已经过了“我”#39;我'正确。
for (int i=0; i<=6; i++) {
var bg(i) = $('.section('+ i +') .container').css("background-image");
var bg2 = $('.section(2) .container').css("background-image");
var bg3 = $('.section(3) .container').css("background-image");
var bg4 = $('.section(4) .container').css("background-image");
var bg5 = $('.section(5) .container').css("background-image");
var bg6 = $('.section(6) .container').css("background-image");
$("#sectionImage1 img").hover(function(){
$('.section(1) .container').addClass('roll1');
}, function(){
$('.section(1) .container').removeClass('roll1');
$('.section(1) .container').css({
'background-image': 'url(' + bg1 + ')'
});
});
$("#sectionImage2 img").hover(function(){
$('.section(2) .container').addClass('roll2');
}, function(){
$('.section(2) .container').removeClass('roll2');
$('.section(2) .container').css({
'background-image': 'url(' + bg2 + ')'
});
})
//... and so on, done 6 times (never more).
}
答案 0 :(得分:0)
像
这样的东西function bindHover( counter )
{
var bg = $('.section('+ counter +') .container').css("background-image");
$("#sectionImage"+ counter +" img").hover(function(){
$('.section('+ counter +') .container').addClass('roll'+ counter +'');
}, function(){
$('.section('+ counter +') .container').removeClass('roll'+ counter +'');
$('.section('+ counter +') .container').css({
'background-image': 'url(' + bg + ')'
});
});
}
然后在for循环中调用它