Why does the code have an error?
var images = ['/test/img/Gallery/l开发者_高级运维arge/4cba0c8a-4acc-4f4a-9d71-0a444afdf48d.jpg','/test/img/Gallery/large/4cba0ca8-2158-41af-829a-0a444afdf48d.jpg','/test/img/Gallery/large/4cbc549a-5228-433f-b0bc-0a444afdf48d.jpg'];
$('.triggerNext').click(function(){
nextImage();
return false;
});
function nextImage(){
currentImage = $('.Pagepage:eq(0)').val();
nextImage = parseInt(currentImage)+1;
$('#imageCurrent').attr('src',images[nextImage]);
$('#imageCurrent') .css('position','absolute').css('left',($(window).width()- $('#imageCurrent').width() )/2);
$('.Pagepage').val(nextImage);
}
It runs correctly the first time but gets an error after clicking.
Yet, the code below runs fine without any errors:
var images = ['/test/img/Gallery/large/4cba0c8a-4acc-4f4a-9d71-0a444afdf48d.jpg','/test/img/Gallery/large/4cba0ca8-2158-41af-829a-0a444afdf48d.jpg','/test/img/Gallery/large/4cbc549a-5228-433f-b0bc-0a444afdf48d.jpg'];
$('.triggerNext').click(function(){
currentImage = $('.Pagepage:eq(0)').val();
nextImage = parseInt(currentImage)+1;
$('#imageCurrent').attr('src',images[nextImage]);
$('#imageCurrent') .css('position','absolute').css('left',($(window).width()- $('#imageCurrent').width() )/2);
$('.Pagepage').val(nextImage);
return false;
});
It seems like your're overwritting your function
here:
nextImage = parseInt(currentImage)+1;
Either change the name from your function
or variable
. Even better, don't use a global namespace
. Anyway, you can't overwrite the name
from a function, your're overwritting the function itself.
After the above line, nextImage
contains a Number
which obviously cannot get executed.
right from the comments
just replace nextImage = parseInt(currentImage)+1; with var nextImage = > parseInt(currentImage)+1; – Alin Purcaru
Using a var
statement, also avoids the nextImage
from going into the global namespace.
use:
var nextImage = parseInt(currentImage)+1;
so you don't overwrite your function name
精彩评论