[英]Setting height, and width of an image from vars in javascript
我正在嘗試將圖像作為背景並適合瀏覽器的尺寸。 但是,我得到: expmrt.html:15 Uncaught TypeError: Cannot set property 'width' of null
。
//Get the browser legth and height, then after the code is done, time out in 300 miliseconds. function ScreenLoop() { var Width = window.innerWidth; var Height = window.innerHeight; alert("Width: " + Width + " and also Height: " + Height) //Debugging reasons document.getElementById("theimage").width = Width; // Where the error: expmrt.html:15 Uncaught TypeError: Cannot set property 'width' of null) occures for some reason. document.getElementById("theimage").height = Height; setTimeout(ScreenLoop(), 300); }; ScreenLoop();
<img src="tl2fZkc.png" id="theimage" width="50" height="50" alt="theimage" />
從窗口調用load事件內的ScreenLoop()
window.onload = function() {
ScreenLoop();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.