私はこの問題のために他の投稿を探しています(当然です)。 しかし私はこれらの他のポストの答えでは成功しませんでした。バックグラウンド画像にjavascriptを使用
その後、私の問題:
私はその後、ランダムに
<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>
画像を開くには、次のスクリプトを持って、私は(代わりに、画像/ hochbau.jpgの)この中で、この画像を持っているしたいと思います:
<div class="section22" style="background-image:url(<script> banner(); </script>);"> </div>
:
<div class="section22" style="background-image:url(images/hochbau.jpg); "> </div>
その後、私はこれを試してみました10
またはこの
<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>
その後、私は、スクリプトを変更し、次の
<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])';
} </script>
しかし、何も.....多分私は閉じています作業していません解決策にはまっていますが、私は立ち往生しています...
いくつかの観察があれば、 教えてください !あなたの助け
fiddle please ... – Billz
これは 'document.getElementById( 'myPElement')にする必要があります。style.backgroundImage = 'url(' + img [n] + ')'; –
可能な複製のhttps:// stackoverflow .com/questions/15231812 /ランダム背景画像-ssss3 – Sagar