这是一种用jquery的数据存储克隆状态并随时恢复的方法。无论您对元素所做的更改次数如何。 例如,
http://jsfiddle.net/xr63r/
HTML
<div id="commentform">
<input type="text" class="red-border" />
<button class="comment-reply-link">test</button>
</div>
<button class="undo">undo</button>
JS
$(".comment-reply-link").click(function() {
if($("#commentform").width() < 590){
/*create a clone of the element you modify*/
var theClone = $("#commentform").clone(true,true);
/*add the cloned element to a data attribute for restoring the state*/
$("#commentform").data("undo",theClone);
/*any kind of css modifications may follow*/
$("#commentform input[type='text']").css("margin-left","0");
$("#commentform input[type='text']").removeAttr('class');
}
})
$(".undo").click(function(){
/*if state undo exists, restore it*/
if($("#commentform").data("undo")){
var theClone = $("#commentform").data("undo");
$("#commentform").replaceWith(theClone);
}
});
CSS
/*this is an example css style related to a class*/
input.red-border{
border:red 1px solid;
}
在任何需要的事件下,您都可以使用相同的代码将值重置回其先前的值。 –
按下“回复”按钮后,可以使用setTimeout()自动恢复。 –