[英]Why the table can't be get after jumping into another page?
同一目錄中有兩個網頁:test.html和model.html。
這是test.html上js代碼的一部分,一個showResult函數與一個onclick Envet綁定在一起。
function showResult(){
location.href="model.html";
var _table=document.getElementById("showTable");
for(var i=0;i<sessionStorage.length;i++){
var newRow=_table.insertRow(0);
var cell0=newRow.insertCell(0);
var cell1=newRow.insertCell(1);
vname=sessionStorage.key(i);
vtime=sessionStorage.getItem(vname);
cell0.innerHTML=vname;
cell1.innerHTML=vtime;
}
}
//the body part of test.html
<input id="show" type="button" value="show result" onclick="showResult()">
該model.html如下。
<table id="showTable" border="2px">
<tr>
<th>name</th>
<th>time</th>
</tr>
<tr>
</table>
//the body part of model.html
<input type="button" value="return" onclick="window.history.go(-1)">
我想從sessionStorage讀取數據並顯示在ID為showTable的表中。
單擊以觸發test.html中的showResult函數時,網頁從test.html跳至model.html,發生錯誤。
TypeError: _table is null
1. test.html
seesionStorage中的兩個元素。
a1
5
和a2
6
2.要單擊test.html上的show result
按鈕,網頁跳入model.html
,並在test.html
seesionStorage中添加兩個元素。
a1
5
和a2
6
傳遞到新網頁-model.html中。 3.查看在model.html
控制台中發生了什么。
對於此處的示例,sessionStorage已將benn從test.html傳遞到model.html,但無法將sessionStorage上的所有元素寫入到model.html中id為showTable的表中。
為什么?
為什么無法通過document.getElementById("showTable");
獲取表 從test.html跳到model.html之后?
一旦行location.href="model.html";
執行,瀏覽器將被重定向到model.html頁面,並且隨后的代碼將不被執行。
您應將以下所有代碼移至model.html頁面,因為重定向后您將無法從另一頁面的另一頁面訪問HTML元素
var _table=document.getElementById("showTable");
for(var i=0;i<sessionStorage.length;i++){
var newRow=_table.insertRow(0);
var cell0=newRow.insertCell(0);
var cell1=newRow.insertCell(1);
vname=sessionStorage.key(i);
vtime=sessionStorage.getItem(vname);
cell0.innerHTML=vname;
cell1.innerHTML=vtime;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.