如何保存 HTML 中的更改?
How to save changes in HTML?
我知道如何向浏览器中当前加载的 HTML 文件添加内容,现在我只想知道是否有办法将这些更改保存到 .html 文件本身。基本上当文件关闭并稍后再次加载时,它应该仍然具有动态添加的内容。
您可以使用 Session Storage.
window.onload = function () {
if (sessionStorage.getItem("savedItem"))
document.querySelector("#store").value = sessionStorage.getItem("savedItem");
document.querySelector("#store").onkeyup = function (event) {
if (event.keyCode == 13) {
sessionStorage.setItem("savedItem", document.querySelector("#store").value);
alert("Saved!");
}
};
};
<div sandbox="allow-same-origin allow-scripts allow-popups allow-forms">
<input type="text" id="store" placeholder="Type something and press Enter to save..." />
</div>
你不能单独使用 HTML。将服务器端脚本语言(例如 PHP)与数据库(例如 MySQL)一起使用。将您的页面内容存储在数据库中,并使用服务器端脚本将内容提供给您的 html 页面。
(也可以使用XML文件来存储数据,不需要数据库)
如果您需要存储更多 complex/large 量的数据,还有 indexedDB
,客户端。不需要服务器端脚本。
SessionStorage 和 LocalStorage 仅存储字符串,因此如果您需要存储对象,则必须先将其字符串化。但是对于 indexedDB 它不会。
它比 sessionStorage 复杂得多(在这里解释太多),但这些链接在我研究它时对我有所帮助。
基本上你连接到数据库,处理任何需要的升级(这处理第一次创建和设置事务),然后你得到你的对象存储,最后执行你的读取,更新等。大部分工作是在数据库的设置和升级中。
https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
http://code.tutsplus.com/tutorials/working-with-indexeddb--net-34673
我知道如何向浏览器中当前加载的 HTML 文件添加内容,现在我只想知道是否有办法将这些更改保存到 .html 文件本身。基本上当文件关闭并稍后再次加载时,它应该仍然具有动态添加的内容。
您可以使用 Session Storage.
window.onload = function () {
if (sessionStorage.getItem("savedItem"))
document.querySelector("#store").value = sessionStorage.getItem("savedItem");
document.querySelector("#store").onkeyup = function (event) {
if (event.keyCode == 13) {
sessionStorage.setItem("savedItem", document.querySelector("#store").value);
alert("Saved!");
}
};
};
<div sandbox="allow-same-origin allow-scripts allow-popups allow-forms">
<input type="text" id="store" placeholder="Type something and press Enter to save..." />
</div>
你不能单独使用 HTML。将服务器端脚本语言(例如 PHP)与数据库(例如 MySQL)一起使用。将您的页面内容存储在数据库中,并使用服务器端脚本将内容提供给您的 html 页面。
(也可以使用XML文件来存储数据,不需要数据库)
如果您需要存储更多 complex/large 量的数据,还有 indexedDB
,客户端。不需要服务器端脚本。
SessionStorage 和 LocalStorage 仅存储字符串,因此如果您需要存储对象,则必须先将其字符串化。但是对于 indexedDB 它不会。
它比 sessionStorage 复杂得多(在这里解释太多),但这些链接在我研究它时对我有所帮助。
基本上你连接到数据库,处理任何需要的升级(这处理第一次创建和设置事务),然后你得到你的对象存储,最后执行你的读取,更新等。大部分工作是在数据库的设置和升级中。
https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API http://code.tutsplus.com/tutorials/working-with-indexeddb--net-34673