How to load some html and append to <body> with jQuery?

浪尽此生 提交于 2019-11-28 10:45:13

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... :)

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'
});

An alternative solution:

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

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

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?

Here you go:

<script type="text/javascript">
    $(function() {
        $("body").load("Views/chatBox.html");
    });
</script>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!