[英]onload event event javascript not responding
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的
second.jpg
意思是“ second
對象的jpg
屬性”(之前沒有提到過,因此將引發引用錯誤)。
您需要使用字符串文字: "second.jpg"
。
second.jpg應該用引號引起來,
<script type="text/javascript">
function react() {
document.getElementById("img").src = 'second.jpg';
}
</script>
字符串值必須在引號之間。 另外,您應該在<body>
標簽中進行廣告onload
事件。
document.getElementById(“ img”)。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括起來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.