每次刷新页面时如何更改背景图像? [英] how can change background image when page every refresh?
本文介绍了每次刷新页面时如何更改背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个div标签,这个标签有背景图片,style.css有这个图像位置等。
怎么办这个图像会在每个页面刷新。我想我会使用javascript但我没有做
there is a div tag this tag has background image and style.css has this image position etc.
how can I do this image will be change when every page refresh.I think I will use javascript but I didnt do
<div id="background-container">
<%--<img alt="" src="images/1.jpg">--%>
</div>
这个我s div标签我试试这个
this is div tag I try this
<script type="text/javascript">
function getHelloThere() {
document.getElementById('background-container').ChangeIt();
}
</script>
<script type="text/javascript">
var totalCount = 6; I have image folder 6 image for background
function ChangeIt() {
var num = Math.ceil(Math.random() * totalCount);
document.body.background = 'images/' + num + '.jpg';
}
</script>
但没有做什么
but nothing do
推荐答案
试试这样..
Try like this..
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript">
var loading = function () {
var totalCount = 6;
var num = Math.floor(Math.random() * totalCount) + 1
document.body.background = 'images/' + num + '.jpg'
}
</script>
</head>
<body onload=loading();>
<form id="form1" runat="server">
</form>
</body>
</html>
尝试
Try
<html>
<head>
<script type="text/javascript">
var totalCount = 6;
function ChangeIt()
{
var num = Math.ceil( Math.random() * totalCount );
document.getElementById('background-container').style.background = 'images/'+num+'.jpg';
}
</script>
</head>
<body onload='ChangeIt()'>
</body>
</html>
这篇关于每次刷新页面时如何更改背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文