如何使用 JavaScript / jQuery 获取 <html> 标签 HTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4196971/
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
How to get the <html> tag HTML with JavaScript / jQuery?
提问by Justin Stayton
Using $('html').html()
I can get the HTML within the <html>
tag (<head>
, <body>
, etc.). But how can I get the actual HTML of the <html>
tag (with attributes)?
使用$('html').html()
我可以得到内的HTML<html>
标签(<head>
,<body>
,等)。但是如何获得<html>
标签的实际 HTML (带有属性)?
Alternatively, is it possible to get the entire HTML of the page (including doctype, <html>
, etc.) with jQuery (or plain old JavaScript)?
或者,是否可以<html>
使用 jQuery(或普通的旧 JavaScript)获取页面的整个 HTML(包括 doctype 、 等)?
回答by Michael
The simplest way to get the html
element natively is:
html
本地获取元素的最简单方法是:
document.documentElement
Here's the reference: https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement.
这是参考:https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement。
UPDATE: To then grab the html
element as a string you would do:
更新:然后将html
元素作为字符串抓取,您将执行以下操作:
document.documentElement.outerHTML
回答by posit labs
This is how to get the html DOM element purely with JS:
这是纯粹使用 JS 获取 html DOM 元素的方法:
var htmlElement = document.getElementsByTagName("html")[0];
or
或者
var htmlElement = document.querySelector("html");
And if you want to use jQuery to get attributes from it...
如果您想使用 jQuery 从中获取属性...
$(htmlElement).attr(INSERT-ATTRIBUTE-NAME);
回答by doubleswirve
In addition to some of the other answers, you could also access the HTML element via:
除了一些其他答案之外,您还可以通过以下方式访问 HTML 元素:
var htmlEl = document.body.parentNode;
Then you could get the inner HTML content:
然后你可以得到内部的 HTML 内容:
var inner = htmlEl.innerHTML;
Doing so this way seems to be marginally faster. If you are just obtaining the HTML element, however, document.body.parentNode
seems to be quite a bit faster.
这样做似乎稍微快一点。但是,如果您只是获取 HTML 元素,则document.body.parentNode
似乎要快一些。
After you have the HTML element, you can mess with the attributes with the getAttribute
and setAttribute
methods.
拥有 HTML 元素后,您可以使用getAttribute
和setAttribute
方法来处理属性。
For the DOCTYPE, you could use document.doctype
, which was elaborated upon in this question.
对于 DOCTYPE,您可以使用document.doctype
,这在这个问题 中有详细说明。
回答by doubleswirve
In jQuery:
在 jQuery 中:
var html_string = $('html').outerHTML()
In plain Javascript:
在纯 Javascript 中:
var html_string = document.documentElement.outerHTML
回答by jordanstephens
if you want to get an attribute of an HTML element with jQuery you can use .attr();
如果您想使用 jQuery 获取 HTML 元素的属性,您可以使用 .attr();
so $('html').attr('someAttribute');
will give you the value of someAttribute
of the element html
所以$('html').attr('someAttribute');
会给你someAttribute
元素的值html
Additionally:
此外:
there is a jQuery plugin here: http://plugins.jquery.com/project/getAttributes
这里有一个 jQuery 插件:http: //plugins.jquery.com/project/getAttributes
that allows you to get all attributes from an HTML element
允许您从 HTML 元素中获取所有属性