gpt4 book ai didi

jQuery,如何克隆回保存的元素?

转载 作者:行者123 更新时间:2023-12-01 04:54:05 24 4
gpt4 key购买 nike

要进行演示,请参阅我的 html:

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
See it:
<pre>
print "Say hello"
</pre>
<p><b>==SEP==</b></p>
<pre>
class Voila {
public:
// Voila
static const string VOILA = "Voila";

// will not interfere with embedded <a href="#voila1">tags</a>.
}
</pre>
</html>

在 Chrome v26 控制台中,我执行:

var pres_orig=$('pre').clone()
$('pre').replaceWith('<pre>spam</pre>')

然后我的问题:如何克隆回<pre>保存的 pres_orig 中的内容以便我的页面显示其原始内容?

我尝试了以下方法,但不起作用:

$('pre').replaceWith(pres_orig)

enter image description here enter image description here

最佳答案

因为您有多个没有标识值的 pre 元素。您可以采取的一种方法是循环所有元素并将内部 html 存储在数组中。然后,当您需要再次加载它们时,您可以循环它们并使用索引拉取数组 html。

类似这样的事情:

//store all the data on load
var storage = [];
$("pre").each(function(){
storage.push($(this).html());
});

//set the new html to "spam"
$("pre").html("spam");

//reload the original data
$("pre").each(function(index){
$(this).html(storage[index]);
});

Here is a working example

关于jQuery,如何克隆回保存的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16147598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com