0

我正在尝试使用 for 循环创建导航,在 actionscript 中我通常使用 for 循环来执行此操作。我想为每个链接分配相同的操作(scrollTop),但目标不同(#sec1,#sec2 ...)。但是在javascript中我不明白我错在哪里。

一个例子:

var Secs = [
    "sec1",
    "sec2",
    "sec3",
    "sec4",
    "sec5",
    "sec6"
];

for(var i = 0; i < Secs.length; i++){
    $("." + this.Secs).click(function(){
        $('html, body').animate({scrollTop: $("#" + this.Secs).offset().top}, 700);
    });
}

谢谢。

4

1 回答 1

1

你不能使用this.Secs-你可以像这样Secs使用数组Secs[i]

 $("." + Secs[i]).click(function(){
        $('html, body').animate({scrollTop: $("#" + Secs[i]).offset().top}, 700);
 });
于 2013-05-25T09:15:49.637 回答