0

タイトルにあるように、Web ページでこの JavaScript 関数を呼び出す方法を見つけようとしています。これは私のビジネス用で、テンプレートは基本的な無料のものです。私よりも経験豊富な人にとっては、おそらく正しくフォーマットするだけの簡単な問題だと思います。これが私が取り組んでいるものです。

Web ページの HEAD 部分に入るコード:

var theImages = new Array() 
theImages[0] = 'splash1.jpg'
theImages[1] = 'splash2.jpg'
theImages[2] = 'splash3.jpg'
theImages[3] = 'splash4.jpg'
theImages[4] = 'splash5.jpg'
theImages[5] = 'splash6.jpg'

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
  preBuffer[i] = new Image()
  preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

</script>

次に、使用する関数を呼び出します。

<SCRIPT LANGUAGE="JavaScript">
showImage();
</script>

これを実装しようとしているページは次のとおりです。 http://coloradopp.com/index4.html

画像を表示するだけでなく、その関数を呼び出したいと思います。スプラッシュ 1 ~ 6 はすべて元の画像と同じサイズです。

コード スニペットは次のとおりです。

<div id="splash">
<img class="pic" src="images/splash1.jpg" width="870" height="374" alt="" />
</div>

ご覧のとおり、ページはすべての書式設定のためにスタイル シート (style.css) を呼び出します。

これを機能させる方法について誰かがヒントを提供できますか? 私が集めたものから、javascript を css シートに実装することはできません。前もって感謝します。

4

3 に答える 3

1

このようなことをします:

showImage() {
  var theImages = [ 'splash1.jpg', 'splash2.jpg', 'splash3.jpg', 'splash4.jpg', 'splash4.jpg' ];
  var img = theImages[Math.round(Math.random() * (theImages.length - 1))];
  document.getElementById('splash').innerHTML = '<img src="' + img + '">');
}
于 2012-05-13T16:38:40.913 に答える
0

画像を背景画像として動的に設定し、このようなものを配置できます

<script>
document.write('<style>#splash{ background-image:url(\'images/splash'+Math.round(Math.random()*5+1)+'.jpg\');}</style>');
</script>

あなたのページの先頭に。このソリューションでは、div タグ (870x374) の固定サイズを設定する必要があります。

于 2012-05-13T16:15:56.017 に答える
0

最初に、JavaScript コードを次のように関数内に移動します。

function showImage(){ ...your code goes here...}

そして、次のようにページの読み込み時に関数を開始できます。

<body onload="showImage()">
于 2012-05-13T16:01:02.180 に答える