0
Неисправность кода
Подскажите, что не так... var gran=document.getElementById("gran"); gran.style.height = "10px"; данный код вызывает сообщение:Uncaught TypeError: Cannot read property 'style' of null Line: 2 https://code.sololearn.com/WYWnAE8VFbX8/?ref=app
7 Answers
+ 5
Yes. It's waits the DOM elements to be fully loaded.
'onload' is an event listener. You can put it in the html element, to wait for specific element.
Example:
HTML:
<body>
<div id="gran" onload="myFunc()">
</div>
</body>
JS:
function myFunc() {
var gran = document.getElementById("gran");
gran.style.height = 10px;
}
+ 4
// Put the JS code inside the function
window.onload = function() {
// Your code here
};
+ 2
Thank you so much! As I understand it, my code tried to execute before the full load of html and css? Can I use "window.onload" without a function?
+ 2
thank you again.
0
New problem) You need to slow down the action, but setInterval does not work. Any thoughts?
window.onload=function() {
var gran=document.getElementById("gran");
var y=function () {1+2}
var x=0;
while (x<90)
{x=x+1 ; setInterval(y,2000);
gran.style.marginLeft=x+"px";};};
0
corrected himself)