如何使用 DOM 中的元素更新 HTML 字符串

How to update an HTML string with an element from DOM

我正在尝试使用 DOM 中的元素更新 stringify HTML 元素。 此 stringify HTML 元素位于 localStorage 的数组中。

首先,这是我如何将其转换为可操作的 HTML :

let toBeUpdated = document.createElement('div');
toBeUpdated.innerHTML = `${var_stringify_html_from_localstorage}`;
toBeUpdated = toBeUpdated.firstElementChild;

toBeUpdated 元素是一个 div,其中包含一个 ID 为 updateMe 的元素。 我想用 DOM.

中的元素更新此“虚拟”(不可见)#updateMe 元素

我先试了:

let updateme = toBeUpdated.querySelector("#updateMe");
let updateme_DOM = document.querySelector("#updateMe");

toBeUpdated.replaceChild(updateme, updateme_DOM);

哪个returnsUncaught DOMException: Node.replaceChild: Child to be replaced is not a child of this node

然后我尝试了 :

let updateme = toBeUpdated.querySelector("#updateMe");
let updateme_DOM = document.querySelector("#updateMe");

updateme.replaceWith(updateme_DOM);

从 DOM 中删除原始 #updateMe 并且不更新 toBeUpdated 元素。

我显然遗漏了什么,但我看不到什么...

我尽量重现我的情况:

//initial state of local storage
let motd = ["<div id='toBeUpdated'><h1>Hello World</h1><p id='updateMe'>Message of tomorrow</p></div>"];
window.localStorage.setItem('motds', JSON.stringify(motd));

// My situation, later in the code
let motds = JSON.parse(window.localStorage.getItem('motds'));
let toBeUpdated = document.createElement('div');
toBeUpdated.innerHTML = `${motds[0]}`;
toBeUpdated = toBeUpdated.firstElementChild; // this is the div#toBeUpdated

let DOMUpdateMe = document.getElementById('updateMe');
let storageUpdateMe = toBeUpdated.querySelector("#updateMe"); // getElementById doesn't work here

// Replace
storageUpdateMe.replaceWith(DOMUpdateMe);

// Back in local storage
motds[0] = toBeUpdated.outerHTML;
window.localStorage.setItem('motds', JSON.stringify(motds));
<body>
  <div id="toBeUpdated">
    <h1>Hello World</h1>
    <p id="updateMe">Message of tomorrow</p>
  </div>
</body>

它使用 firstChild 而不是 firstElementChild

let toBeUpdated = document.createElement('div');
//toBeUpdated.innerHTML = `${var_stringify_html_from_localstorage}`;
toBeUpdated.innerHTML = `<div id="updateMe">New DIV</div>`;
let updateme = document.getElementById('updateMe')
updateme.replaceWith(toBeUpdated.firstChild);
<div id='updateMe'>Old DIV</div>

你不应该使用简单的 newUpdateMe = DOMUpdateMe;

//initial state of local storage
let motd = ["<p class='updateMe'>Message of the day<p>"];
window.localStorage.setItem('motds', JSON.stringify(motd));

// My situation, later in the code
let motds = JSON.parse(window.localStorage.getItem('motds'));
let dOMUpdateMe = document.querySelectorAll('#toBeUpdated > *');

let counter = 0;
dOMUpdateMe.forEach(function(dOMToUpdate) {
  console.log(dOMToUpdate);
  if(dOMToUpdate.classList.contains('updateMe')) {
    motds[counter] = dOMToUpdate.outerHTML;
    counter++;
  }
})
window.localStorage.setItem('motds', JSON.stringify(motds));
<div id="toBeUpdated">
  <h1 class="updateMe">Hello World</h1>
  <p class="updateMe">Update Me</p>
  <p class="no">No not Update Me</p>
  <p class="no">No not Update Me</p>
  <p class="no">No not Update Me</p>
  <p class="updateMe">Update Me too</p>
  <p class="no">No not Update Me</p>
</div>

看来您唯一缺少的是在将 DOM 节点插入到非 DOM.

之前克隆它

这是您的代码的分步复制,仅更改此代码即可。为了清楚起见,我还将 localStorage 的初始状态设置为“今天的消息”。

localStorage 是用变量模拟的,因为它会因 CORS

在代码段内触发安全错误

//initial state of local storage
let motd = ["<div id='toBeUpdated'><h1>Hello World</h1><p id='updateMe'>Message of today</p></div>"];
//window.localStorage.setItem('motds', JSON.stringify(motd));

//Read from local storage
//let motds = JSON.parse(window.localStorage.getItem('motds'));
const motds = JSON.parse(JSON.stringify(motd)); //Simulate local storage
console.log("In storage:", motds);

//Create element outside of DOM
const toBeUpdated = document.createElement('div');
toBeUpdated.innerHTML = motds[0];
console.log("To be updated:", toBeUpdated.outerHTML);

//Get part to update
const DOMUpdateMe = document.getElementById('updateMe');
const storageUpdateMe = toBeUpdated.querySelector("#updateMe");
console.log(DOMUpdateMe, storageUpdateMe);
//Replace part outside of DOM with clone of part from DOM
storageUpdateMe.replaceWith(DOMUpdateMe.cloneNode(true));
console.log(toBeUpdated.outerHTML);

//Store back
motds[0] = toBeUpdated.outerHTML;
motd = JSON.stringify(motds); //Simulate local storage
console.log(motd);
//window.localStorage.setItem('motds', JSON.stringify(motds));
<body>
  <div id="toBeUpdated">
    <h1>Hello World</h1>
    <p id="updateMe">Message of tomorrow</p>
  </div>
</body>