jQuery error when using image slider?
I have an image slider (NivoSlider) which simply fades through a range of images on our website. It's working fine on the homepage with the following code:
HEAD of INDEX.PHP
<!-- Image Slider -->
<link rel="stylesheet" href="_ui/css/slider.css" type="text/css" media="screen" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="_ui/js/jquery.nivo.slider.pack.js" type="text/javascript"></script>
<script type="text/javascript">
$(window).load(function() {
$('.nivoSlider').nivoSlider({
effect:'fade', // Spec开发者_运维百科ify sets like: 'fold,fade,sliceDown'
slices:15, // For slice animations
boxCols: 8, // For box animations
boxRows: 4, // For box animations
animSpeed:500, // Slide transition speed
pauseTime:2000, // How long each slide will show
startSlide:0, // Set starting Slide (0 index)
directionNav:false, // Next & Prev navigation
directionNavHide:false, // Only show on hover
controlNav:false, // 1,2,3... navigation
controlNavThumbs:false, // Use thumbnails for Control Nav
controlNavThumbsFromRel:false, // Use image rel for thumbs
controlNavThumbsSearch: '.jpg', // Replace this with...
controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
keyboardNav:true, // Use left & right arrows
pauseOnHover:false, // Stop animation while hovering
manualAdvance:false, // Force manual transitions
captionOpacity:0.8, // Universal caption opacity
prevText: 'Prev', // Prev directionNav text
nextText: 'Next', // Next directionNav text
beforeChange: function(){}, // Triggers before a slide transition
afterChange: function(){}, // Triggers after a slide transition
slideshowEnd: function(){}, // Triggers after all slides have been shown
lastSlide: function(){}, // Triggers when last slide is shown
afterLoad: function(){} // Triggers when slider has loaded
});
});
</script>
DIV OF SLIDER IN INDEX.PHP
<div class="nivoSlider">
<img src="_media/img/home_first_final.jpg" alt="" />
<img src="_media/img/home_second_final.jpg" alt="" />
<img src="_media/img/home_third_final.jpg" alt="" />
</div>
The above code is working fine and can be reviewed here.
When I try and re-use the code on a sub-content page I get the following error:
[cycle] terminating; zero elements found by selector
Uncaught TypeError: Object [object Object] has no method 'nivoSlider'
(anonymous function)
c.event.handle
c.event.add.j.handle.o
I checked the code on the sub-content pages (an example page here) and the code is now an exact duplicate of the HEAD of INDEX.PHP but the DIV is as follows:
<div class="nivoSlider">
<img src="_media/img/services_first_final.jpg" alt="" />
<img src="_media/img/services_second_final.jpg" alt="" />
<img src="_media/img/services_third_final.jpg" alt="" />
</div>
Any idea what's going wrong?
You are loading jQuery twice on the second page.
You have this near the end of the page
<script type="text/javascript" src="_ui/js/jquery-1.5.min.js"></script>
Well, the error sis telling you that there's no "nivoSlider" method on a certain object.
Looking into your code, the object it's trying to call that method on is: $('.nivoSlider')
Is it possible that when you load your subpage that the item that has the nivoSlider class either doesn't exist - or is something that you didn't expect?
Or (FTW) that there is more than one of them... and thus it's returning an array (instead of a single object) and thus needs to be run using "each"
I had this same problem.
Check to see that any included scripts do not include the line:
jQuery.noConflict();
This will break scripts that use the $ jQuery alias.
精彩评论