onload事件javascript没有响应
Javascript代码:onload事件javascript没有响应,javascript,Javascript,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" sty
<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>
请注意,中的图像不会更改,因为它将在15000毫秒内从first.jpg更改为second.jpg,这是由setTimeout函数设置的。jpg表示第二个对象的jpg属性,之前未提及过,因此它将抛出一个引用错误
您需要使用字符串文字:second.jpg。second.jpg应该在引号中
<script type="text/javascript">
function react() {
document.getElementById("img").src = 'second.jpg';
}
</script>
字符串值必须介于引号之间。此外,您应该在标记中添加ad onload事件 document.getElementByIdimg.src='second.jpg'
应引用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>
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。您需要学习如何调试JavaScript。有一个明显的错误,应该会在错误控制台中产生某种输出。img有限地支持onload,这实际上使您的代码片段非常精彩。目前OP的代码创建了一个无限循环,每15秒重新加载一次图像。您可以在firefox浏览器中使用firebug进行调试