如果我在下面的代码中将文本输入标记分配给 idstored_data1,我是否能够在客户端保存我的数据(例如将其保存在闪存驱动器上)?
HTML
Data that is retrieved will appear here
<div id="dataStore"></div>
JS
<script>
if (typeof Storage !== 'undefined') {
localStorage.setItem('stored_data1', 'Blue Box');
document.getElementById('dataStore').innerHTML =
localStorage.getItem('stored_data1');
} else {
// in case web storage is not supported
document.getElementById('dataStore').innerHTML =
'Web storage not supported.';
}
</script>
上面的代码是 HTML 的 Web 存储 API。我希望找到一种方法最终将其融入到我制作的一些程序中。
最佳答案
您可以使用以下代码下载标签上的信息
//create a function to download text to textfile
function downloadText(text, filename){
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
}
//get Text Value then download
var textValue = document.getElementById("test").innerText;
download(textValue, "store_data");
关于javascript - 保存 HTML 数据客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70639429/