javascript - 用户离线时如何将 xml 文件保存到本地存储并更新值?
问题描述
我正在尝试编写货币转换器。我正在从存储货币和汇率的 url 下载 xml 文件。我想将货币保存到本地存储中,这样当用户离线时,他仍然可以进行转换。我在控制台中打印 xml 文件只是为了检查我是否正在获取 xml 文件。所以我的问题是,我如何将货币和汇率存储到本地存储中,以及当用户再次在线时如何更新这些值?javascript:
function display(e ) {
if ((e.target !== e.currentTarget) && e.target.id !== "clear" && e.target.id !== "kati") {
var clickedItem = e.target.id;
document.getElementById("display").value = document.getElementById("display").value + clickedItem;
}
else if (e.target.id = "clear" && e.target.id !== "kati") {
document.getElementById("display").value = null;
} else if (e.target.id = "kati") {
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'https://devweb2018.cis.strath.ac.uk/~aes02112/ecbxml.php');
ourRequest.onreadystatechange = function () {
var data = ourRequest.responseText;
console.log(data);
};
ourRequest.send();
}
e.stopPropagation();
}
xml 文件的 url 显示在代码中。
解决方案
是的,正如 Chris 已经说过的,您可以使用默认的 localStorage:
localStorage.setItem("hello","world");
localStorage.getItem("hello"); //world
例如,在您收到回复后
ourRequest.onreadystatechange = function () {
if (ourRequest.readyState === 4) {
if (xhr.status == 200){
if (ourRequest.status == 200){
var data = ourRequest.responseXML;
var someTag = data.getElementsByTagName("someTag");
//something like this
localStorage.setItem("someTagValue",someTag[0].childNodes[0].nodeValue);
}
}
};
推荐阅读
- css - 我在输入焦点上有一个标签的过渡,有没有办法让过渡在模糊时倒退?
- python - 使用 draw() 而不是 eventloop 时的 pyglet
- java - mybatis 避免插入相同的数据
- angular - 如果我的对象为空且没有接口键,则 TypeScript 类型断言
- jquery - Bootstrap 4 响应性无法正常工作
- android - 创建一个影响内部视图的圆形视图
- javascript - 如何在 3D 球体周围环绕文本
- spring-boot - 如何在密码中为 Spring Neo4j 查询实现条件关系子句?
- c++ - 为 x86 系统编译 Readline(静态模式)
- python - 在 QGraphicsView 中显示 X 和 Y 坐标线