gpt4 book ai didi

javascript - 背景图片没有变化

转载 作者:行者123 更新时间:2023-12-02 18:34:08 24 4
gpt4 key购买 nike

我正在尝试获取随机背景图像。但是当我加载页面时,我的背景是空的。这是我的代码:

<html>
<head>
<script>
function random_imglink(){
var myimages=new Array();
myimages[1]="images/1.png";
myimages[2]="images/2.png";
myimages[3]="images/3.png";
myimages[4]="images/4.png";
var count=Math.floor(Math.random() * myimages.length);
if (count==0){
count=1;
}
document.write("'" + myimages[count] + "'");
}
</script>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>

<body background=random_imglink()>
<?php include ("header.php"); ?>
<div align="center" >
<?php include ("main.php"); ?>
<?php include ("footer.php"); ?>
</div>
</body>
</html>

我希望有人能解决这个问题?

最佳答案

将其添加到脚本标记中:

window.onload = function () {
var imgs = [
'images/1.png',
'images/2.png',
'images/3.png',
'images/4.png'
];
document.body.style.background = 'url(' + imgs[Math.round(Math.random() * (imgs.length - 1))] + ')';
}

How to set background in css

不要忘记检查图像是否确实存在。

关于javascript - 背景图片没有变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17501580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com