我想将HTML div(及其所有内容)放入JSON对象并将其保存到PouchDB数据库中。
我的div看起来像这样:
<div class="easel">
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: rgb(85, 222, 25);"></div><div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: rgb(164, 101, 52);"></div><div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: rgb(214, 15, 77);"></div><div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
</div>
我写了一个这样的函数:
window.saveBoard = function() {
var db = new PouchDB('tiles');
var remoteCouch = false;
var current_layout = $('.easel');
var new_board = {
_id: new Date().toISOString(),
board: current_layout
}
db.put(new_board, function callback(err, result) {
if(!err) {
console.log("Successfully saved new board.");
}
})
}
但它似乎没有做我想做的事情,而且我在控制台中收到此错误:Uncaught DataCloneError: Failed to execute 'put' on 'IDBObjectStore': An object could not be cloned.
我需要做些什么来存储此div信息,以便稍后将其恢复到页面中?
答案 0 :(得分:2)
您无法直接在PouchDB中存储HTML元素;你只能存储直接的JSON。因此,您需要在存储之前将div转换为某种数据结构格式。