Html5 Web 存储未在第二页中显示数据
Html5 web storage not displaying data in a second page
我正在使用网络存储来保存数据。当我通过表单获取数据并将其保存在变量(在本例中为"一")并尝试在同一页面中显示时,它会正确执行此操作。但是当我导入相同的".js"文件并尝试在不同的页面中显示它时,它不起作用。如何使用网络存储来检索不同页面中的数据?另外,我不想使用查询字符串!
第一页:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="webstorageAPI.js"></script>
<link href="webstorageAPI.css" rel="stylesheet" type="text/css" />
</head>
<body>
<section id="leftbox">
<section id="leftbox">
<form action="seconddisplayfile.html" method="post">
<p>(key) One: <input type="text" id="one" /></p>
<p>(value)Two:<textarea id="two"></textarea></p>
<p><input type="submit" id="button" value="Save" /></p>
</form>
</section>
<section id="rightbox">
Nothing yet !
</section>
</section>
</body>
</html>
第二页:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="webstorageAPI.js"></script>
<link href="webstorageAPI.css" rel="stylesheet" type="text/css" />
</head>
<body>
<section id="wbox">
Nothing yet!
</section>
</body>
</html>
Javascript代码:
// JavaScript Document
function doFirst(){
var button = document.getElementById("button");
button.addEventListener('click',save,false);
}
function save(){
var one = document.getElementById("one").value; //but only contents is to be taken not label n all
var two = document.getElementById('two').value;
localStorage.setItem("one",two); //Store in key-value pair. Anytime you use this you use if by addressing the variable ie key name.
//Retriveing data n displaying stored data
display(one); //now u hav stored two so its time to use one
}
function display(one){
var rightbox = document.getElementById('rightbox'); //refer right box
var two= localStorage.getItem("one"); //Get in key-value pair
rightbox.innerHTML= "Name of variable:"+one+"<br/>Value:"+two;
/*----Second page display-----*/
var wbox = document.getElementById("wbox");
wbox.innerHTML= "Name of variable:"+one+"<br/>Value:"+two;*/
}
window.addEventListener('load',doFirst,false);
@charset "utf-8";
/* CSS Document */
#leftbox{
float:left;
padding:20px;
border:3px solid #F20B84;
}
#rightbox{
float:left;
width:250px;
margin-left:20px;
padding:20px;
border:3px solid #8E1783;
}
您的第二页不包含任何 id rightbox
.so 的元素
var rightbox = document.getElementById('rightbox');
将返回null
.并且当尝试设置innerHTML
rightbox
的属性时,js 会抛出
Uncaught TypeError: Cannot read property 'innerHTML' of null
因此,请将display
更改为
function display(one){
var rightbox = document.getElementById('rightbox'); //refer right box
var two= localStorage.getItem("one"); //Get in key-value pair
if(rightbox)
rightbox.innerHTML= "Name of variable:"+one+"<br/>Value:"+two;
/*----Second page display-----*/
var wbox = document.getElementById("wbox");
wbox.innerHTML= "Name of variable:"+one+"<br/>Value:"+two;*/
}
您必须在第二页中调用display
。
相关文章:
- 带有显示XML数据的HTML的空白页
- 如何在emberjs中使用幻影假数据进行分页
- 如何在分页事件中突出显示数据表中的单词
- 为什么数据不能't显示在查看页中
- 如何在Java Script中比较二维数组和一维数组,并将常见数据存储在另一个数组中
- 数据表不显示与表的分页
- 如何使用引导程序分页器库对从PHP返回的数据进行分页
- 如何使用javascript检测数据表分页的其他页面中的复选框
- 为什么分页数据表JQuery没有'不要使用JSF ui:重复
- 复选框只适用于第一页-数据表、轨道
- RXJS 中的分页数据光标以及对 subject.onDone 和错误的混淆
- 使用承诺以递归方式检索分页数据
- 从mysql中分页数据表,自动刷新
- 使用Backbone Collection一次获取多页数据
- 使用promise收集未知大小的分页数据
- 从不在当前分页数据表上的文本框中检索值
- 模式窗口不能为分页数据表工作
- 窗口.print在ie11中没有打印表中的所有分页数据
- 屏幕抓取分页数据
- 本地存储保存表单N页数据,直到最后提交