-1

Javascript代码:

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

请注意,图像中的图像不会改变,因为它是在 setTimeout() 函数设置的 15000 毫秒内从 first.jpg 变为 second.jpg

4

5 回答 5

1

second.jpg表示“对象的jpg属性second”(之前没有提到,所以它会抛出一个引用错误)。

您需要使用字符串文字:"second.jpg"

于 2013-08-28T10:01:15.183 回答
1

second.jpg 应该用引号引起来,

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

字符串值必须在引号之间。此外,您应该在标签中广告onload事件。<body>

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

于 2013-08-28T10:01:59.610 回答
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

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>

此外,请在您的反应函数中用引号将 second.jpg 括起来。

于 2013-08-28T10:04:02.457 回答