为什么$(document).html()不能获取整个html字符串?还有其他方法吗

why $(document).html() cannot get whole html string?Is there other methods to get this?

本文关键字:html 其他 字符串 方法 document 不能 获取 为什么      更新时间:2023-09-26

$(document).html() 

返回空字符串,而不是整个html字符串。如何获取?

您可以使用Javascript outerHTML属性轻松完成,无需jquery代码。

简单使用:document.documentElement.outerHTML

这将为您提供整个HTML以及HTML标记及其相关属性。

如果你想使用jQuery,请使用:-

$('html')[0].outerHTML

它相当简单,只需使用

$('html').html()

我没有测试这个。。为什么不试试

    $('html').html();

尝试

$(document).text() 

Demo:打开chrome的inspect元素(开发人员的工具)并键入以下内容。。您将看到结果:)