How can jQuery load some html and append it to the <body> element?

后端 未结 5 491
小蘑菇
小蘑菇 2020-12-10 10:37

I tried the following:

$.load(\"Views/chatBox.html\").appendTo(\'body\')

Console Output:

TypeError: $.load is not a functio         


        
相关标签:
5条回答
  • 2020-12-10 11:10

    Here you go:

    <script type="text/javascript">
        $(function() {
            $("body").load("Views/chatBox.html");
        });
    </script>
    
    0 讨论(0)
  • 2020-12-10 11:14

    I dont understand why placing container at the bottom of body and loading external page into that is not what you need?

    What you can try is this:

    <script type="text/javascript">
        $(function() {
            $("#container").load("Views/chatBox.html",function(){
                $(this).clone().appendTo("body").remove();
            });
        });
    </script>
    

    But im not 100% sure about this code... :)

    0 讨论(0)
  • 2020-12-10 11:20

    Nope, all those answers are incorrect because they rely on having a separate container!

    Do this:

    $.ajax({
        url: "your.html",
        success: function (data) { $('body').append(data); },
        dataType: 'html'
    });
    
    0 讨论(0)
  • 2020-12-10 11:31

    When I tried out GaVrA's solution I found it could be even simpler:

    <script type="text/javascript">
        $(function() {
            $("#container").load("external.html").appendTo("body");;        
        });
    </script>
    

    I guess appentTo automatically removes a previous instance? Perhaps I'm missing something here?

    0 讨论(0)
  • 2020-12-10 11:33

    An alternative solution:

    jQuery('#AppendToMe').append( jQuery('<div>').load(...) );
    

    This will append whatever you load into the #AppendToMe element.

    0 讨论(0)
提交回复
热议问题