How do I get the entire page's HTML with jQuery?

后端 未结 5 723
有刺的猬
有刺的猬 2020-11-27 04:45

I used $(document).html(), but that threw an error... is there a way to get everything?

5条回答
  •  囚心锁ツ
    2020-11-27 05:09

    Don't forget the tag can have attributes too. If you want the whole document this should work.

     $('html')[0].outerHTML
    

    It's also trivial without jQuery.

    document.documentElement.outerHTML
    

    If you also want to include the doctype, it's a little more involved.

    var getDocTypeAsString = function () { 
        var node = document.doctype;
        return node ? "\n' : '';
    };
    
    getDocTypeAsString() + document.documentElement.outerHTML   
    

提交回复
热议问题