BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / www-technology / #16819同步于 2012/7/13
该镜像源已超过 30 天没有更新,可能在源站已被删除。
WWWTechnology机器人发帖

[讨论][问题]js控制div随机切换背景图片问题

byrcjh
2012/7/13镜像同步2 回复
function changeBackImage(){ var photoArray =new Array(5); photoArray[0] = '../css/image/bg1.jpg'; photoArray[1] = '../css/image/bg2.jpg'; photoArray[2] = '../css/image/bg3.jpg'; photoArray[3] = '../css/image/bg4.jpg'; photoArray[4] = '../css/image/bg5.jpg'; /*photoArray[Math.floor(Math.random()*5)]*/ document.getElementById('mailBody').style.backgroundImage='url(photoArray[Math.floor(Math.random()*5)])'; } setInterval(changeBackImage,1000); 我想通过这段代码实现div中背景的动态随机切换,但是并没有读取到图片,(图片的地址没有问题),不知道有没有人可以帮忙解决一下,谢谢!
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
embubian机器人#1 · 2012/7/17
改成这样就行了document.getElementById('mailBody').style.backgroundImage='url('+photoArray[Math.floor(Math.random()*5)]+')'; 因为photoArray[Math.floor(Math.random()*5)]是表达式不能直接加在字符串中
byrcjh机器人#2 · 2012/7/21
嗯,却是是这样子,谢谢哈 【 在 embubian 的大作中提到: 】 : 改成这样就行了document.getElementById('mailBody').style.backgroundImage='url('+photoArray[Math.floor(Math.random()*5)]+')'; 因为photoArray[Math.floor(Math.random()*5)]是表达式不能直接加在字符串中