简体   繁体   English

如何将 document.body 保存到变量并再次从该变量加载

[英]How Save document.body to variable and load from that variable again

The $("body").load("myUrl") function in jQuery allows the html content of a file to be loaded into the body, in which case all the scripts in the html file in the body are loaded and executed correctly. The $("body").load("myUrl") function in jQuery allows the html content of a file to be loaded into the body, in which case all the scripts in the html file in the body are loaded and executed correctly. How to provide html content from a variable to body instead of loading it from a file.如何将 html 内容从变量提供到正文,而不是从文件中加载。 For example, consider that:例如,考虑一下:

in index.html :index.html中:

  <script>
        window.keeper = null;  


        function callForm2() {

            window.keeper = window.document.body;

            $("body").load(
                "Form2.html"
            );

        }


        function closeForm2() {

            window.document.body = window.keeper;

        }
    </script>

in callForm2 Before the body content is called from a file, the current body content stored in a window.keeper variable.callForm2从文件中调用正文内容之前,当前正文内容存储在一个 window.keeper 变量中。

in closeForm2() , I want to take the content from the window.keeper variable (old body content) and set to current body, in which case the content of the variable is not equal to the previously saved one, and I can not return the original content of the variable.closeForm2()中,我想从 window.keeper 变量(旧正文内容)中获取内容并设置为当前正文,在这种情况下,变量的内容不等于之前保存的内容,我无法返回变量的原始内容。

So how do I save the current body content of a variable for the next call?那么如何保存变量的当前正文内容以供下次调用? What is the correct way to save and call in this case?在这种情况下保存和调用的正确方法是什么?

here is my code: (you can also see https://github.com/yarandish/Challenge2 )这是我的代码:(您还可以看到https://github.com/yarandish/Challenge2

I Solved you problem and putting demo here.我解决了你的问题并将演示放在这里。 Your Solution is here: https://github.com/Master2V/FormCallSolution.git您的解决方案在这里: https://github.com/Master2V/FormCallSolution.git

In index.html put this script in head:在 index.html 中,将此脚本放在头中:

    <script>

    window.keeper = null;

    function callForm2() {

        var $body_page = $('body #Page');
        window.keeper = $body_page;
        $body_page.detach();

        $.ajax({
            type: "GET",
            url: "Form2.html",
            data: "",
            dataType: "html",
            success: function (response) {
                $("body").html(response);
            }
        });
    }

    function closeForm2() {

        var $body_page = $('body #Page');
        $body_page.detach();
        
        var $body = $('body');
        $body.append(window.keeper);

    }

</script>

And you must have a div tag with id="Page" in index.html and any other forms that you want to call.并且您必须在 index.html 和您要调用的任何其他 forms 中有一个 id="Page" 的 div 标签。

Consider the following.考虑以下。

 window.keeper = {}; $(function() { $("#psudo_body > p").click(function() { window.keeper.html = $("#psudo_body").html(); window.keeper.text = $("#psudo_body").text(); console.log("Stored", window.keeper); var form = $("<form>", { id: "myForm" }); $("<button>", { type: "submit" }).html("Done").appendTo(form); $("#psudo_body").html(form); }); $("body").on("submit", "#myForm", function(event) { event.preventDefault(); $("#psudo_body").html(keeper.html); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="psudo_body"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In condimentum elit sit amet mi molestie, ac commodo nibh blandit. Proin a tellus efficitur dui commodo varius. Nullam massa nunc, mollis in finibus ac, volutpat eu urna. Maecenas quis sodales felis, non finibus lorem. Aliquam tristique, dolor vitae consequat commodo, augue sem porta ex, eu tincidunt lectus orci ut elit. Integer nec enim eu erat pulvinar pretium eu id lorem. Sed et dapibus ligula, a rhoncus augue. Fusce et purus in diam aliquet accumsan. Curabitur mattis, ante quis tempor luctus, ante sapien dignissim metus, sit amet maximus ante lorem eu turpis. Praesent in sodales velit. Suspendisse varius ornare faucibus. Suspendisse maximus erat sodales, tincidunt nulla sit amet, fermentum ligula. Donec sed sollicitudin nunc. Phasellus vitae mauris bibendum, fringilla orci id, sodales libero. Donec at ullamcorper lectus. Proin dui enim, venenatis quis euismod vulputate, blandit sit amet nibh.</p> </div>

This would work for body element as well.这也适用于body元素。

You can also use .data() in a similar way.您也可以以类似的方式使用.data()

 $(function() { $("#psudo_body > p").click(function() { $("#psudo_body").data("keeper", $("#psudo_body").html()); var form = $("<form>", { id: "myForm" }); $("<button>", { type: "submit" }).html("Done").appendTo(form); $("#psudo_body").html(form); }); $("body").on("submit", "#myForm", function(event) { event.preventDefault(); $("#psudo_body").html($("#psudo_body").data("keeper")); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="psudo_body"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In condimentum elit sit amet mi molestie, ac commodo nibh blandit. Proin a tellus efficitur dui commodo varius. Nullam massa nunc, mollis in finibus ac, volutpat eu urna. Maecenas quis sodales felis, non finibus lorem. Aliquam tristique, dolor vitae consequat commodo, augue sem porta ex, eu tincidunt lectus orci ut elit. Integer nec enim eu erat pulvinar pretium eu id lorem. Sed et dapibus ligula, a rhoncus augue. Fusce et purus in diam aliquet accumsan. Curabitur mattis, ante quis tempor luctus, ante sapien dignissim metus, sit amet maximus ante lorem eu turpis. Praesent in sodales velit. Suspendisse varius ornare faucibus. Suspendisse maximus erat sodales, tincidunt nulla sit amet, fermentum ligula. Donec sed sollicitudin nunc. Phasellus vitae mauris bibendum, fringilla orci id, sodales libero. Donec at ullamcorper lectus. Proin dui enim, venenatis quis euismod vulputate, blandit sit amet nibh.</p> </div>

See Also: Storing a variable in the JavaScript 'window' object is a proper way to use that object?另请参阅: 在 JavaScript '窗口' object 中存储变量是使用 object 的正确方法吗?

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

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