简体   繁体   English

如何复制html元素,文本和样式中的所有内容

[英]How to copy everything in an html elements, text and style

I have a jQuery page that I built. 我有一个自己构建的jQuery页面。 The crucial parts all lie within a div like so.. 关键部分都像这样位于div内。

<div id="TestDiv"></div>
<div id="randomDialogName"></div>
<div id="randomDialogName"></div>
<div id="randomDialogName"></div>
<div id="randomDialogName"></div>
</body>

within the TestDive there can be lots of elements, charts, grids, live handlers and etc. (Mostly jQueryUI Elements) 在TestDive中,可以有很多元素,图表,网格,实时处理程序等(大多数jQueryUI元素)

What I would like to do is select everything within "TestDiv" copy it to a variable as a temporary measure then when I load the data back into TestDiv, I would like to reload everything the way it was. 我想做的是选择“ TestDiv”中的所有内容,并将其复制到变量中以作为一种临时措施,然后在将数据加载回TestDiv中时,我想重新加载所有内容。 Like so. 像这样

function saveDivElements(){
    var saveId = $('#TestDiv').attr('tab');
    var html = $('#TestDiv').html(); //have tried clone(true) and other methods here
    console.log(html);
    setLoaded(saveId, true, html); //saves to divData member variable on page
}

$("#TestDiv").html(divData); // called later and loads data

What I have currently, saves the elements, but there is no class information or handlers that are saved. 我目前所拥有的保存了元素,但是没有保存任何类信息或处理程序。 So on reload the elements are there, but nothing else remains. 因此,在重新加载时,元素就在那里了,但别无其他。 I need something where I don't have to reload all the handlers, reset all the buttons. 我需要一些不需要重新加载所有处理程序,无需重置所有按钮的东西。 (Also this runs locally, performance is not an issue) (这也可以在本地运行,性能不是问题)

Can you utilize .detach() instead of extracting the raw HTML? 您可以利用.detach()代替提取原始HTML吗?

var savedChildren = $('#TestDiv').children().detach();

It takes along with it anything that you have bound to the elements. 它结合了您绑定到元素的任何内容。 Here is a basic example in jsfiddle . 这是jsfiddle中的一个基本示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM