繁体   English   中英

使用javascript作为背景图片

[英]Use javascript for a background image

我正在寻找其他帖子来解决这个问题(因为那里当然有)。 但是我什至没有成功回答其他帖子。

然后,我的问题:

我有以下脚本来随机打开图像

<script> function banner()
{var img = new Array();
img[0]='images/hochbau1.jpg';
img[1]='images/hochbau2.jpg';
img[2]='images/hochbau3.jpg';
img[3]='images/hochbau4.jpg';
img[4]='images/hochbau5.jpg';
img[5]='images/hochbau6.jpg';
img[6]='images/hochbau7.jpg';
img[7]='images/hochbau8.jpg';
img[8]='images/hochbau9.jpg';
img[9]='images/hochbau10.jpg';
var n=Math.round((Math.random()*9));
document.write(img[n]);
} </script> 

然后,我想在其中使用这些图像(而不是images / hochbau.jpg):

 <div class="section22" style="background-image:url(images/hochbau.jpg); "> </div>

然后,我尝试了这个:

 <div class="section22" style="background-image:url(<script> banner(); </script>);"> </div> 

或这个

<div class="section22" style="background-image:url(
                   <script type="text/javascript" language="javascript">
                       document.write('images/hochbau'+ Math.round((Math.random()*9)+1)+ '.jpg');
                  </script>
                  ); "> </div>

然后,我将脚本更改为:

<script> function banner()
{var img = new Array();
img[0]='images/hochbau1.jpg';
img[1]='images/hochbau2.jpg';
img[2]='images/hochbau3.jpg';
img[3]='images/hochbau4.jpg';
img[4]='images/hochbau5.jpg';
img[5]='images/hochbau6.jpg';
img[6]='images/hochbau7.jpg';
img[7]='images/hochbau8.jpg';
img[8]='images/hochbau9.jpg';
img[9]='images/hochbau10.jpg';
var n=Math.round((Math.random()*9));
document.getElementById('myPElement').style.backgroundImage = 'url(img[n])';

} </script> 

与以下

<div class="section22" id="myPElement">

但是什么都没有起作用.....也许我不了解该解决方案,但是我被困住了...

如果您有任何观察,请告诉我! 非常感谢您的帮助

您只是在脚本中没有正确引用图像

<script> function banner()
{var img = new Array();
img[0]='images/hochbau1.jpg';
img[1]='images/hochbau2.jpg';
img[2]='images/hochbau3.jpg';
img[3]='images/hochbau4.jpg';
img[4]='images/hochbau5.jpg';
img[5]='images/hochbau6.jpg';
img[6]='images/hochbau7.jpg';
img[7]='images/hochbau8.jpg';
img[8]='images/hochbau9.jpg';
img[9]='images/hochbau10.jpg';
var n=Math.round((Math.random()*9));
document.getElementById('myPElement').style.backgroundImage = 'url(' + img[n] +')';  //you are passing img[n] as string, just change this

} </script> 

编辑:我看到您的页面: http : //www.mytmedia.craym.eu/HRPwebsite/index.php

从代码检查中,我看到banner()函数从未执行过,而且如果我尝试在控制台中执行它,则背景图像设置正确,但是您的图像不存在: 在此处输入图片说明

这应该工作

document.getElementById("myPElement").style.backgroundImage = "url("+img[n]+")";

嗨,非常感谢您的帮助。

我的代码中出错的地方是:1-在脚本中使用了函数。 我只需要使用脚本但不使用“功能” 2,当然“ url(img [n])”的形式是错误的。

最适合我的代码(我已经在本地测试过,我不得不在线测试过)

                <div id="myPElement" class="section22" ></div>
            <script> 
                var img = new Array();
                img[0]='http://localhost:8888/images/hochbau1.jpg';
                img[1]='http://localhost:8888/images/hochbau2.jpg';
                img[2]='http://localhost:8888/images/hochbau3.jpg';
                img[3]='http://localhost:8888/images/hochbau4.jpg';
                img[4]='http://localhost:8888/images/hochbau5.jpg';
                img[5]='http://localhost:8888/images/hochbau6.jpg';
                img[6]='http://localhost:8888/images/hochbau7.jpg';
                img[7]='http://localhost:8888/images/hochbau8.jpg';
                img[8]='http://localhost:8888/images/hochbau9.jpg';
                img[9]='http://localhost:8888/images/hochbau10.jpg';
                var n=Math.round((Math.random()*9));
                document.getElementById("myPElement").style.backgroundImage = "url(" + img[n] +")";
            </script> 

Evrything正在运行...。照片随机出现。

非常感谢您的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM