简体   繁体   English

我怎样才能“重置”<div> 被 JavaScript 修改后恢复到原来的状态?

[英]How can I "reset" <div> to its original state after it has been modified by JavaScript?

I have a DIV with a form in it.我有一个带有表单的 DIV。 When users submit the form and it gets successfully submitted, I replace the form with a simple "everything is good now" message:当用户提交表单并成功提交时,我用一个简单的“现在一切都很好”消息替换表单:

$("#some_div").html("Yeah all good mate!");

Is there a good way to "reset" the div to its "original state" as per the HTML it has arrived with?有没有一种好方法可以根据它随附的 HTML“重置”div 到它的“原始状态”? I can only think of actually doing something like this:我只能想到实际做这样的事情:

//before I change the DIV
var originalState = $("#some_div").html();
//manipulate the DIV
//...
//push the state back
$("#some_div").html(originalState);

It doesn't look very elegant - I guess there is a better solution for this, no?它看起来不是很优雅 - 我想有一个更好的解决方案,不是吗?

I would clone the element, instead of saving the content.我会克隆元素,而不是保存内容。 Then use replaceWith to restore it:然后使用replaceWith来恢复它:

var divClone = $("#some_div").clone(); // Do this on $(document).ready(function() { ... })

$("#some_div").html("Yeah all good mate!"); // Change the content temporarily

// Use this command if you want to keep divClone as a copy of "#some_div"
$("#some_div").replaceWith(divClone.clone()); // Restore element with a copy of divClone

// Any changes to "#some_div" after this point will affect the value of divClone
$("#some_div").replaceWith(divClone); // Restore element with divClone itself

You can use the data attribute to save the state rather than a variable您可以使用 data 属性来保存状态而不是变量

$('#some_div').data('old-state', $('#some_div').html());
$('#some_div').html($('#some_div').data('old-state'));

What you're doing is not optimal.你正在做的不是最优的。 The best solution would be this:最好的解决方案是这样的:

When the form gets successfully submitted, just hide() the FORM element, and show() the message (which is initially hidden).当表单成功提交时,只需hide() FORM 元素,并show()消息(最初是隐藏的)。 And then, later, just show() the FORM element and hide() the message.然后,稍后,只需show() FORM 元素并hide()消息。

Yeah, the way you have is the way to do it.是的,您拥有的方式就是这样做的方式。 The DOM does not save the previous states of DIVs, so you need to save that yourself. DOM 不会保存 DIV 的先前状态,因此您需要自己保存。

In my opinion best is to use this:在我看来最好是使用这个:

var originalState = $("#some_div").clone();
$("#some_div").replaceWith(originalState.clone());

This way you can repeatedly use this to restore your div to original state while keeping the data in "originalState" intact.这样你就可以反复使用它来将你的 div 恢复到原始状态,同时保持“originalState”中的数据完好无损。 Worked for me.为我工作。

Somewhat more elegant?优雅一点?

var originalState = $("#some_div").clone();
$("#some_div").replaceWith(originalState);

You have basically three options.你基本上有三个选择。

  1. Remember your original markup, as you do with your originalState variable above.记住您的原始标记,就像您对上面的originalState变量所做的一样。
  2. Use AJAX to re-request the markup.使用 AJAX 重新请求标记。 You can do this easily if you have server side code using the $.ajax() method in jQuery.如果您有使用 jQuery 中的$.ajax()方法的服务器端代码,则可以轻松完成此操作。
  3. Cause the page to reload.导致页面重新加载。

在 jQuery 中调用empty函数就可以了

$(".div").empty();

This is my very first interaction on this site--I can't comment yet, but this is to @Fleuv's comment on @Josiah Ruddell's answer:这是我在这个网站上的第一次互动——我还不能发表评论,但这是@Fleuv 对@Josiah Ruddell 的回答的评论:

The default parameter for .clone() is "false", which will not clone event listeners. .clone()的默认参数是“false”,它不会克隆事件监听器。 If you make it .clone(true) it clones the event listeners as well.如果您将其设置为.clone(true) ,它.clone(true)克隆事件侦听器。 I've tested it (with the rest of his answer) and it works.我已经对其进行了测试(使用他的其余答案)并且它有效。

w3schools jQuery clone() method w3schools jQuery clone() 方法

var originalState = $("#some_div").clone(true, true);
$("#some_div").replaceWith(originalState.clone(true, true));

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

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