為什么我跟著老師寫,還是錯了?
window.onload?=?function(){
var?container?=?document.getElementById('container');
var?photos?=?document.getElementById('photos');
var?buttons?=?document.getElementById('buttons');
var?prev?=?document.getElementById('prev');
var?next?=?document.getElementById('next');
var?index?=?1;
function?showButton(){
buttons[index-1].className?=?'on';
}
function?animate(offset){
var?newLeft?=?parseInt(photos.style.left)?+?offset;
photos.style.left?=?newLeft?+?'px';
if(newLeft?>?-600){
photos.style.left?=?-3000?+?'px';
}
if(newLeft?<?-3000){
photos.style.left?=?-600?+?'px';
}
}
next.onclick?=?function(){
index?+=?1;
showButton();
animate(-600);
}
prev.onclick?=?function(){
index?-=?1;
animate(600);
showButton();
}
}就是加上圓點的函數后開始出錯的。。。
2016-07-19
var buttons = document.getElementById('buttons').getElementsByTagName("span");
這一句錯了,獲取的是buttons下面的五個按鈕,而不是buttons。