简体   繁体   English

onload事件事件javascript没有响应

[英]onload event event javascript not responding

Javascript code: JavaScript代码:

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

Html Page: 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>

Please the The image in the does not change as it is meant to change from first.jpg to second.jpg in 15000 milliseconds as set by the setTimeout() function 请中的图像保持不变,因为它是按照setTimeout()函数设置的在15000毫秒内从first.jpg更改为second.jpg的

second.jpg means "The jpg property of the second object" (which is hasn't been mentioned before so it will throw a reference error). second.jpg意思是“ second对象的jpg属性”(之前没有提到过,因此将引发引用错误)。

You need to use a string literal: "second.jpg" . 您需要使用字符串文字: "second.jpg"

second.jpg should be in quotes, second.jpg应该用引号引起来,

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

The string value must be between quotes. 字符串值必须在引号之间。 Also, you should ad onload event in <body> tag. 另外,您应该在<body>标签中进行广告onload事件。

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

second.jpg should be quoted and the setTimeout accepts a function reference: 应该引用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>

The IMG tag does nopt support the onload attribute. IMG标签不支持onload属性。 Move it to the body tag: 将其移动到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>

Also, enclose second.jpg in your react function in quotes. 另外,在您的react函数中用引号将second.jpg括起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM