-1

Javascript コード:

<script type="text/javascript">
  function react() {
     document.getElementById("img").src = second.jpg
  }
</script>

HTML ページ:

<div style="width:170px;height:160px;border:2px solid blue">
 <img src="first.jpg" style="width:inherit;height:inherit" id="img" onload="setTimeout('react()', 15000)"/>
</div>

setTimeout() 関数で設定された 15000 ミリ秒で first.jpg から second.jpg に変更されるため、 の画像は変更されません。

4

5 に答える 5

1

IMG タグは onload 属性をサポートしていません。body タグに移動します。

<body onload="setTimeout('react()', 15000)">
  <div style="width:170px;height:160px;border:2px solid blue">
   <img src="first.jpg" style="width:inherit;height:inherit" id="img" />
  </div>
</body>

また、react 関数の second.jpg を引用符で囲みます。

于 2013-08-28T10:04:02.457 に答える
1

second.jpgjpg「オブジェクトのプロパティ」を意味しsecondます(これは以前に言及されていないため、参照エラーがスローされます)。

文字列リテラルを使用する必要があります: "second.jpg".

于 2013-08-28T10:01:15.183 に答える
1

second.jpg は引用符で囲む必要がありsetTimeout、関数参照を受け入れます。

<script type="text/javascript">
  function react() {
     document.getElementById("img").src = "second.jpg"
  }
</script>

<div style="width:170px;height:160px;border:2px solid blue">
 <img src="first.jpg" style="width:inherit;height:inherit" id="img" onload="setTimeout(react, 15000);"/>
</div>
于 2013-08-28T10:02:51.283 に答える
1

second.jpg should be in quotes,

<script type="text/javascript">
 function react() {
  document.getElementById("img").src = 'second.jpg';
 }
</script>
于 2013-08-28T10:01:32.273 に答える
1

The string value must be between quotes. Also, you should ad onload event in <body> tag.

document.getElementById("img").src = 'second.jpg';

于 2013-08-28T10:01:59.610 に答える