为了账号安全,请及时绑定邮箱和手机立即绑定

图片轮播没效果

图片轮播没效果

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Document</title><style type="text/css">#d{position: relative;top:-10px;}label{margin-left:20px;}</style></head><script type="text/javascript">var n=0;function init(){n++;if (n==7)n=1;var img=document.getElementById("img");img.src=n+".jpg";setTimeout("init()",1000);   //设置轮播时间}</script><body><div><img src="1.jpg" width="300" height="300"><div id="d"><label>&nbsp;1&nbsp;</label><label>&nbsp;2&nbsp;</label><label>&nbsp;3&nbsp;</label><label>&nbsp;4&nbsp;</label><label>&nbsp;5&nbsp;</label><label>&nbsp;6&nbsp;</label><label>&nbsp;7&nbsp;</label></div></div></body></html>
查看完整描述

2 回答

?
qq_冲哥_0

TA贡献40条经验 获得超30个赞

1, 你在body的上面书写script 标签,也就是js代码, 你没有window.onload, 根本获取不到 img图片。

2, 再者,你的img 没有id, getElementbyId() 也获取不到img 图片。

3, 你只写了一个init()函数,根本没有调用。

4, setTimeout 最好接受一个函数作为第一个参数,这是最佳实践, 不要传字符串


<img src="1.jpg" width="300" height="300"  id ='img'> 


<script type="text/javascript">

var n=0;

var img=document.getElementById("img");

function init(){

n++;

if (n==7)

n=1;

img.src="bg" + n+".jpg";

setTimeout(init,1000);   //设置轮播时间

}

init();


查看完整回答
2 反对 回复 2016-12-03
  • 2 回答
  • 0 关注
  • 1668 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信