javascript Javascript获取当前页面的html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17451679/
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 html of current page
提问by LanguagesNamedAfterCofee
Is it possible to get the html of the page with all modifications made by the user (see below)?
是否可以通过用户所做的所有修改获取页面的 html(见下文)?
For example, if text has been entered into a textarea, or a checkbox has been chosen, that should be reflected in the html as well. My reason for wanting to do this is to take a "screenshot" of the user's page.
例如,如果文本已输入到 textarea 中,或者已选择一个复选框,则这也应反映在 html 中。我想要这样做的原因是对用户页面进行“屏幕截图”。
The answers below that are a variation of jQuery('html')
, do not reflect changes made by the user.
下面的答案是 , 的变体jQuery('html')
,不反映用户所做的更改。
采纳答案by Richard
If you're wanting a screenshot, you should consider a tool such as html2canvas. Just pulling the HTML isn't going to get you the input fields.
如果您需要屏幕截图,则应考虑使用html2canvas等工具。仅拉取 HTML 不会让您获得输入字段。
Another SO thread along these lines.
Other answers will get you the HTML, specifically, but without the inputs' values
其他答案将为您提供 HTML,特别是,但没有输入的值
$('html').html();
回答by Rene Pot
You can use jQuery:
您可以使用 jQuery:
var html = $('html')
回答by musefan
Adapting upon the other answers. Prior to obtaining the HTML, you could loop though all the input elements and manually set the value
property, this would then be reflected in the HTML:
适应其他答案。在获取 HTML 之前,您可以遍历所有输入元素并手动设置value
属性,然后这将反映在 HTML 中:
$('a').click(function(e){
setValueProperties();
var html = getHtml();
alert(html);
});
function setValueProperties(){
$('input').each(function(){
$(this).attr("value", $(this).val());
});
}
function getHtml(){
return document.documentElement.outerHTML;
}
And here is an example that supports checkboxes
NOTE: You may need to refine this function for your exact needs, but hopefully it will get you on track for what you need.
注意:您可能需要根据您的具体需求优化此功能,但希望它能让您按自己的需要走上正轨。
回答by JonLeslieHarding
If you have a field named "field":
如果您有一个名为“field”的字段:
var field = document.getElementById("field");
fieldvalue= field.value;
If you have a checkbox, the operation is literally the same. Hope this was helpful.
如果您有一个复选框,则操作实际上是相同的。希望这是有帮助的。