此JS尝试将html元素保存到myHTML
。但稍后当该元素改变时,该变量也会改变。
如何保存myHTML
的副本以备后用?将html元素保存到变量
let myHTML = '';
$('td.label').each(function() {
if (this.textContent === "sign:") {
if (!myHTML) myHTML = this.parentElement;
return false;
}
});
//later the elemnt gets changed
$('td.label').each(function() {
if (this.textContent === "sign:") {
this.parentElement.appendChild(some-html-element)
return false;
}
});
console.log(myHTML); //it includes the some-html-element
http://stackoverflow.com/questions/921290/is-it-possible-to-clone-html-element-objects-in-javascript-jquery –
并非总是如此,@NJDawson https://开头jsfiddle.net/z481LL1z/1/ – eisbehr
你需要整个html对象吗?如果没有,那么你可以使用html字符串。 myHTML = this.parentElement.html() – Indra