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

10th Dec 2017, 11:39 AM
Илья Ланин
Илья Ланин - avatar
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; }
10th Dec 2017, 2:46 PM
Boris Batinkov
Boris Batinkov - avatar
+ 4
// Put the JS code inside the function window.onload = function() { // Your code here };
10th Dec 2017, 12:55 PM
Boris Batinkov
Boris Batinkov - avatar
+ 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?
10th Dec 2017, 2:03 PM
Илья Ланин
Илья Ланин - avatar
+ 2
thank you again.
10th Dec 2017, 2:48 PM
Илья Ланин
Илья Ланин - avatar
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";};};
10th Dec 2017, 4:50 PM
Илья Ланин
Илья Ланин - avatar
0
corrected himself)
11th Dec 2017, 2:25 PM
Илья Ланин
Илья Ланин - avatar