随机的变换背景颜色

renaski 2003-10-11 06:29:52
<script language="javascript">
function a(){
var bg=new Array()
bg[0]={bgColor:'blue',bgImg:'1.gif'}
bg[1]={bgColor:'red',bgImg:'2.gif'}
bg[2]={bgColor:'green',bgImg:'3.gif'}
index=Math.floor(Math.random()*bg.length);
document.write(bg[index].bgColor+" "+bg[index].bgImg);
document.body.bgColor=bg[index].bgColor;
document.body.background="icon/icon"+bg[index].bgImg
setTimeout("a()",1000)}
</script>
想要的效果是随机变换背景颜色!
...全文
143 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
renaski 2003-10-11
  • 打赏
  • 举报
回复
顶一下吧
renaski 2003-10-11
  • 打赏
  • 举报
回复
谢谢!
小问题,
<script language="javascript">
var bg=new Array();
bg[0]={bgColor:'blue',bgImg:'1.gif'}
bg[1]={bgColor:'red',bgImg:'2.gif'}
bg[2]={bgColor:'green',bgImg:'3.gif'}
function a(){
// alert('test');
index = Math.floor(Math.random()*bg.length);
document.body.bgColor=bg[index].bgColor;
document.body.background="你的目录"+bg[index].bgImg;
setTimeout("a()",1000)}
window.onload=a()
</script>
他说网页里有错误,但我不知道是那里错了,请告知,谢谢!
jsidiot 2003-10-11
  • 打赏
  • 举报
回复
问题在于document.write(bg[index].bgColor+" "+bg[index].bgImg);
document.write之后,你可以查看源代码,已经不是原来的文件了
所以不会重复a();
只能执行一次
另外我建议你这么写最好
你那样写容易死循环

<script language="javascript">
var bg=new Array();
bg[0]={bgColor:'blue',bgImg:'1.gif'}
bg[1]={bgColor:'red',bgImg:'2.gif'}
bg[2]={bgColor:'green',bgImg:'3.gif'}
function a(){
// alert('test');
index = Math.floor(Math.random()*bg.length);
document.body.bgColor=bg[index].bgColor;
document.body.background="icon/icon"+bg[index].bgImg;
}
setInterval("a()",1000);
</script>
jsidiot 2003-10-11
  • 打赏
  • 举报
回复
还不错
renaski 2003-10-11
  • 打赏
  • 举报
回复
不行吗?是不是在打开这个网页的目录里放入1.gif 2.gif 3.gif啊?
好像不可以吗?但是也没有报错,就是效果实现不了
descreekert 2003-10-11
  • 打赏
  • 举报
回复
你函数可以实现

87,987

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧