For the code structure here
var mydo=sessionStorage.getItem("action");
function to_delete(){
var _table=document.getElementById("showTable");
//omit
}
window.onload=function(){
to_delete();
}
I get desired result. Now rewrite the code structure as below:
var mydo=sessionStorage.getItem("action");
var _table=document.getElementById("showTable");
function to_delete(){
//omit
}
window.onload=function(){
to_delete();
}
An error occur, TypeError: _table is null
.
Why can't set document.getElementById
to be global variable?
答案 0 :(得分:5)
In your rewrite var _table=document.getElementById("showTable");
runs before the document loads so the element does not exist.
Instead declare the global var _table;
outside the function and assign to it in the load event.