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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13471840/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:53:01  来源:igfitidea点击:

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

javascriptjqueryregex

提问by eric.itzhak

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 而不是正则表达式

回答by RGA

Try Below Code:

试试下面的代码:

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

回答by Asad Saeeduddin

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/

回答by Gencebay D.

You can use javascript slicemethod 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);

回答by rahul

may be this can help you

也许这可以帮助你

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

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

回答by MrKukac

It's really simple:

这真的很简单:

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

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

If you wanted to get html of this tag:

如果您想获取此标签的 html:

In JQuery: $(head).html();
Native js: document.head.innerHtml

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

You find nice JQuery refernece here: http://visualjquery.com

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