[英]javascript for loop to navigate sections
我正在嘗試使用for循環創建導航,在動作腳本中,我通常使用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);
});
}
謝謝。
您不能使用this.Secs
因為Secs
是一個數組,所以您可以像這樣使用Secs[i]
$("." + Secs[i]).click(function(){
$('html, body').animate({scrollTop: $("#" + Secs[i]).offset().top}, 700);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.