简体   繁体   English

如何从 html 字符串中获取 head 和 body 标签作为字符串?

[英]How to get head and body tags as a string from html string?

Hey i have an html string i'm having trouble getting the tags from.嘿,我有一个 html 字符串,我无法从中获取标签。

I have tried alot of stuff, here are some :我尝试了很多东西,这里有一些:

var head = $("head",$(htmlString)).html();
var body = $("body",$(htmlString)).html();
var head = $("head",htmlString).html();
var body = $("body",htmlString).html();
var head = $("head",$(htmlString).html()).html();
var body = $("body",$(htmlString).html()).html();
var head = htmlString.match(/<head[^>]*>([^<]+)<\/head>/);
var body = htmlString.match(/<body[^>]*>([^<]+)<\/body>/);
var head = jQuery('<div/>').append(htmlString).find('head').html();
var body = jQuery('<div/>').append(htmlString).find('body').html();

Any many other tries besides that.除此之外还有许多其他尝试。 All of this return "undefined" or "" or jquery object when i try loging it to console.当我尝试将其登录到控制台时,所有这些都返回“未定义”或“”或 jquery 对象。

Could anyone tell me how can i get the body and head tags as a string?谁能告诉我如何将 body 和 head 标签作为字符串获取?

Prefered with jQuery/JS and not regex首选 jQuery/JS 而不是正则表达式

Try Below Code:试试下面的代码:

var head = htmlString.match(/<head[^>]*>[\s\S]*<\/head>/gi);
var body = htmlString.match(/<body[^>]*>[\s\S]*<\/body>/gi);

Since you have well formed HTML, you can create a document and select nodes from it:由于您拥有格式良好的 HTML,您可以创建一个文档并从中选择节点:

var doc = (new DOMParser()).parseFromString(htmlstring,"text/html");
console.log(doc.head.outerHTML);
console.log(doc.body.outerHTML);

Here is a demonstration: http://jsfiddle.net/X3Uq2/这是一个演示: http : //jsfiddle.net/X3Uq2/

In Chrome, you can't use a "text/html" content type, so you have to make an XML document and use getElementsByTagName :在 Chrome 中,您不能使用"text/html"内容类型,因此您必须制作一个 XML 文档并使用getElementsByTagName

var s = new XMLSerializer();
var doc = (new DOMParser()).parseFromString(data,"text/xml");
console.log(s.serializeToString(doc.getElementsByTagName("head")[0]));
console.log(s.serializeToString(doc.getElementsByTagName("body")[0]));

http://jsfiddle.net/X3Uq2/2/ http://jsfiddle.net/X3Uq2/2/

You can use javascript slice method this way:您可以这样使用 javascript slice方法:

 var html = '<!DOCTYPE html>'+ '<html>'+ '<head>' + '<meta name="viewport" content="width=device-width" /> '+ '<title></title>' + '</head>' + '<body>Some Text!</body>' + '</html>' var bEnd, bStart; bStart = html.indexOf("<body"); bEnd = html.indexOf("</body"); var body = html.slice(bStart, bEnd); console.log(body);

may be this can help you也许这可以帮助你

var head = jQuery('<div/>').append(htmlString).find('head').html();

var body = jQuery('<div/>').append(htmlString).find('body').html();

It's really simple:这真的很简单:

JQuery: $(head) JQuery: $(head)
Native js: document.head原生js:document.head

If you wanted to get html of this tag:如果您想获取此标签的 html:

In JQuery: $(head).html();在 JQuery 中: $(head).html();
Native js: document.head.innerHtml原生js:document.head.innerHtml

You find nice JQuery refernece here: http://visualjquery.com您可以在这里找到不错的 JQuery 参考: http ://visualjquery.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM