![](/img/trans.png)
[英]I want to show preloader for non ajax call and hide preloader after resources loaded?
[英]I want to hide something and show the other after document is loaded
我的代碼有什么問題,它給我以下錯誤“ TypeError:無法讀取null的'style'屬性”
var container = document.getElementById('container');
var loading = document.getElementById('loading');
function show(){
loading.style.display = 'none';
container.style.display = 'initial';
}
document.body.onload = show();
Javascript從頁面的頂部開始執行。 失敗的原因有兩個,一個是“容器”和“加載” ID不存在的地方,另一個是您的函數在加載HTML之前執行的地方。
如果將代碼段放在頁面底部,則在HTML加載后,它可能會起作用。
另外,立即執行功能的一種有用方法是
(function() {
//some code
})();
這樣,您不必為document.body.onload
使用額外的行。
您可以嘗試這種方式-
var container = document.getElementById('container'); var loading = document.getElementById('loading'); document.body.onload = function(){ loading.style.display = 'none'; container.style.display = 'initial'; }
試試這個應該起作用
function show(){
var container = document.getElementById('container');
var loading = document.getElementById('loading');
loading.style.display = 'none';
container.style.display = 'initial';
}
document.body.onload = show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.