蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 |
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程


    

蓝森林 http://www.lslnet.com 2006年8月25日 8:28

JS随机图片显示效果输出的问题

[html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
        background-attachment: fixed;
        background-repeat: no-repeat;
        background-position: 90% 90%;
}
-->
</style>
</head>

<body>
随机图片:<br>
当前页的背景图片是随机的,它的位置在页面的右下角.请刷新,查看效果.
</body>
</html>
<script language="javascript" type="text/javascript">
var oImgs=new Array(
        "http://www.blueidea.com/img/common/logo.gif",
        "http://gg.blueidea.com/2005/alipay/208x32.gif",
        "http://gg.blueidea.com/2005/chinaok/ad02-2.gif",
        "http://www.blueidea.com/bbs/images/announce.gif",
        "http://gg.blueidea.com/2005/olay/olay.gif",
        "http://gg.blueidea.com/2005/alipay/760x80.gif"
        );
var n=oImgs.length;
var currIndex=Math.round(Math.random() * n)
//alert(currIndex);
document.body.style.backgroundImage="url("+oImgs[currIndex]+")"
</script>


[/html]

我想输出这种格式

<img src="images/coachclub.jpg" alt="图片说明了">

在这句怎么改:document.body.style.backgroundImage="url("+oImgs[currIndex]+")"

不懂JS,见笑了


[html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
        background-attachment: fixed;
        background-repeat: no-repeat;
        background-position: 90% 90%;
}
-->
</style>
</head>

<body>
<img id="Dimg">
</body>
</html>
<script language="javascript" type="text/javascript">
var oImgs=new Array(
        "http://www.blueidea.com/img/common/logo.gif",
        "http://gg.blueidea.com/2005/alipay/208x32.gif",
        "http://gg.blueidea.com/2005/chinaok/ad02-2.gif",
        "http://www.blueidea.com/bbs/images/announce.gif",
        "http://gg.blueidea.com/2005/olay/olay.gif",
        "http://gg.blueidea.com/2005/alipay/760x80.gif"
        );
var n=oImgs.length;
var currIndex=Math.round(Math.random() * n)
//alert(currIndex);

document.getElementById("Dimg").src=oImgs[currIndex]
</script>
[/html]



谢谢,测试了一下,发现总有一个图片是未知的显示

不过我的问题已解决,采用SQL语句直接随机取出要显示的记录

回复 #3 webworks 的帖子

把zergine代码中的
[code]
var currIndex=Math.round(Math.random() * n)
[/code]
改成
[code]
var currIndex=Math.floor(Math.random() * n)
[/code]

又学到了一招...

收藏先..




Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com