Javascript for(){}和setInterval()不起作用
我想换下一张照片,但我没办法Javascript for(){}和setInterval()不起作用,javascript,html,Javascript,Html,我想换下一张照片,但我没办法 <div class="g-carousel" id="m-carousel"> <a href="http://open.163.com/" class="pciture" target="_blank"><img src="imag/banner1.jpg" ></a> <a href="http://study.163.com/" class="pciture two" target="_b
<div class="g-carousel" id="m-carousel">
<a href="http://open.163.com/" class="pciture" target="_blank"><img src="imag/banner1.jpg" ></a>
<a href="http://study.163.com/" class="pciture two" target="_blank" style="display:none"><img src="imag/banner2.jpg" ></a>
<a href="http://www.icourse163.org/" class="pciture three" target="_blank" style="display:none"><img src="imag/banner3.jpg" ></a>
<div class="button">
<i class="checked"></i><i></i><i></i>
</div>
</div>
<script type="text/javascript">
function showpic() {
var carousel = document.getElementById("m-carousel")
var pciture = carousel.getElementsByClassName("pciture");
for (var i=0 ; i < pciture.length; i++)
if (i>2) i=0;
pciture[i].style.display="none";
pciture[i+1].style.display="block";
}
window.onload=function function_name(argument) {
setInterval("showpic()",5000);
}
</script>
如果你改变一下,也许会有帮助
window.onload=function function_name(argument) {
到
希望有帮助
问候
祝你长寿,繁荣昌盛:-你需要一个全球计数器:
var counter = 0;
function showpic() {
var carousel = document.getElementById("m-carousel")
var pciture = carousel.getElementsByClassName("pciture");
// Hide all the pictures.
for (var i=0 ; i < pciture.length; i++)
{
pciture[i].style.display="none";
}
// Show the picture based on the counter.
pciture[counter].style.display="block";
// Increment the counter ready for next time.
counter++;
// Check if the counter needs to loop back.
if(counter >= pciture.length)
counter = 0;
}
window.onload = function() {
setInterval(showpic, 5000);
}
工作
我建议使用大量可用的jQuery插件中的一个来实现这一点;如果计数器超出元素的长度,还要管理它。您的代码将计数器增加到无穷大,您确定这会起作用吗?
var counter = 0;
function showpic() {
var carousel = document.getElementById("m-carousel")
var pciture = carousel.getElementsByClassName("pciture");
// Hide all the pictures.
for (var i=0 ; i < pciture.length; i++)
{
pciture[i].style.display="none";
}
// Show the picture based on the counter.
pciture[counter].style.display="block";
// Increment the counter ready for next time.
counter++;
// Check if the counter needs to loop back.
if(counter >= pciture.length)
counter = 0;
}
window.onload = function() {
setInterval(showpic, 5000);
}
function showpic() {
var carousel = document.getElementById("m-carousel");
var carouselLength = carousel.getElementsByTagName("a").length;
var pciture = carousel.getElementsByClassName("pciture");
if (i>=carouselLength-1){
pciture[i].style.display="none";
i=0;
pciture[i].style.display="block";
i--;
}else{
pciture[i].style.display="none";
pciture[i+1].style.display="block";
}
i++;
}
var i = 0;
setInterval(showpic,2000);