gpt4 book ai didi

javascript - JQuery - 恢复被替换的元素

转载 作者:行者123 更新时间:2023-11-28 12:35:58 24 4
gpt4 key购买 nike

我有按钮编辑和按钮保存。当我单击 edit 按钮时,它会将我的元素替换为 textboxtextarea。如果我单击按钮 保存,它使用了另一个函数,我如何才能将元素恢复到 replaceWith() 之前的状态?

<div class="will-be-turned-into-texbox"></div>

function edit(){
// Replace above div with texbox
}

function save_edit(){
// Change back texbox to the original div
}

最佳答案

replaceWith docs ,有一个提示:

Description: Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.

(我的重点)

所以...存储返回值,当你想把它放回去时,再次使用 append 或(实际上)replaceWith 把它放回去。

无偿的例子:

var replaced;
$(document).on("click", "#replace", function() {
// Save what's being replaced
replaced = $(this).replaceWith('<button id="restore">Restore</button>');
});
$(document).on("click", "#restore", function() {
// Restore it
$(this).replaceWith(replaced);
});
<button id="replace">Replace</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

请注意委托(delegate)事件处理程序的使用,以避免必须重新连接。

关于javascript - JQuery - 恢复被替换的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27624438/

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