Javascript:获取当前页面 CURRENT 源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13913824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Get current page CURRENT source
提问by indapublic
I have HTML and I need to get page source of this html.
我有 HTML,我需要获取此 html 的页面源代码。
document.documentElement.outerHTML
or
或者
$.ajax({
async: true,
type: 'GET',
cache: false,
url: window.location.href,
success: function(data) {
alert(data);
}
});
is working, but they display originally source. If I change html (by jQuery, for example), they don't read my changes.
正在工作,但它们显示原始来源。如果我更改 html(例如,通过 jQuery),他们不会读取我的更改。
Is it possible read CURRENT source of page?
是否可以读取当前页面源?
回答by Bruno
Tried it on chrome and it works. Use
在 chrome 上试了一下,效果很好。利用
document.documentElement.innerHTML
Funnily enough your code also worked
有趣的是你的代码也有效
document.documentElement.outerHTML
Check out the html printed to the console in this fiddle. It actually contains the changes made by jQuery.
在这个fiddle 中查看打印到控制台的 html 。它实际上包含了 jQuery 所做的更改。
回答by antyrat
Using jQuery you can do this by:
使用 jQuery,您可以通过以下方式执行此操作:
$( 'body' ).html();
For example.
例如。
Or convert to String:
或转换为字符串:
$( 'body' ).html().toString();
回答by Tomer
You just need to grab the element and use the html method:
你只需要获取元素并使用 html 方法:
$('.selector').html();
回答by Venkatesh
Simple solution, source of a document usually embedded in html tag
so use $('html').html()
, you will get everything between html tags.
简单的解决方案,文档的来源通常嵌入在 html 标签中,因此使用$('html').html()
,您将获得 html 标签之间的所有内容。