2017-06-23 4 views
1

私はこの問題のために他の投稿を探しています(当然です)。 しかし私はこれらの他のポストの答えでは成功しませんでした。バックグラウンド画像に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> 

しかし、何も.....多分私は閉じています作業していません解決策にはまっていますが、私は立ち往生しています...

いくつかの観察があれば、 教えてください !あなたの助け

+0

fiddle please ... – Billz

+0

これは 'document.getElementById( 'myPElement')にする必要があります。style.backgroundImage = 'url(' + img [n] + ')'; –

+0

可能な複製のhttps:// stackoverflow .com/questions/15231812 /ランダム背景画像-ssss3 – Sagar

答えて

2

ため 多くのおかげであなたは自分のスクリプトで正しく

<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> 

EDIT画像を参照していない:私はあなたのページを参照してください を:コードからhttp://www.mytmedia.craym.eu/HRPwebsite/index.php

は(私はバナーを参照の点検)私がコンソールで実行しようとすると、バックラウンドイメージは正しく設定されていますが、あなたのiamgesは存在しません:enter image description here

+0

多くのおかげで.... をしようとしています私はあまりにもそれを試して、あなたが投稿した後に再試行したが、それは仕事をdoens't ....私はなぜか分からない! – Timama

+0

コンソールにエラーが表示されますか?バナー機能を実行する前に "myPElement"が定義されていることを確認してください。また、あなたのコード例では、実際に関数を呼び出すことはありませんので、実際にbanner()を実行してください。 – Matus

+0

バナーは大丈夫ですが、私はたくさんのことを試したので "コメント"にあります。 – Timama

0

このshあなたの助けのための

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

多くのありがとう..私もこれを試しています....いいえsucces ... – Timama

+0

機能をデバッグしてみてください。また、コンソールでコードを評価してみることもできます。 – Bhaktuu

0

こんにちは、多くのおかげで仕事ウルド....

私のコードで間違っていたものだった。 1-スクリプト内の関数を使用して。私は、スクリプトを使用しなければならなかったが、フォームのURL(IMG [N] ')2 - 「機能」がなく、もちろんウィッヒは間違っていた...

にコードウィッヒの仕事を私のために(私はローカルでテストしています、 i)は、オンラインでそれをテストしなければならなかった

Evrythingが今取り組んでいる

....写真がランダムに表示され

   <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> 
....あなたの助けのために再び

多くの感謝!

関連する問題