开发者

jQuery: save and restore part of the dom on escape key

In a jQuery callback, I would like to store a status of the dom and to restore this status when escape key is pressed :

$(document).ready(function() {
    // add callbacks
    // TODO : *** store dom ***

    $( ".editable" ).开发者_Python百科click(function () {
        // add other callbacks

        $("#add").keyup(function(e){
            if (e.keyCode == 27) { // escape
                // TODO : *** restore dom ***
            }
        });
    });
});

Is there a way to do it ?


Looking for this?

$(document).ready(function() {
    // add callbacks
    var original = $(".editable").html();

    $(".editable").click(function () {
        // add other callbacks

        $("#add").keyup(function(e){
            if (e.keyCode == 27) { // escape
                $(".editable").html(original);
            }
        });
    });
});

Which element are you trying to save and restore?


You could save the HTML with $('html').html() and then insert that back in after ESC.


We solved the problem by reloading the page with window.location.href="url" :

$(document).ready(function() {
    // add callbacks

    $( ".editable" ).click(function () {
        // add other callbacks

        $("#add").keyup(function(e){
            if (e.keyCode == 27) { // escape
                window.location.href="thisPageUrl";
            }
        });
    });
});

So we don't need to store any status. Just rely on http request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜