如何从HTML文档中仅获取文本(无标记)? [英] How can I get the text only (no tags) from a HTML document?
问题描述
我有一个HTML页面,我只想要文本(所有文本节点)。
I have a HTML page, and I want the text only (all text nodes).
<span>hello <strong>sir</strong></span>
期望输出
Desired Output
hello sir
推荐答案
假设你只想要子元素 body
元素...
Assuming you only want children of body
element...
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> Example</title>
</head>
<body>
a <div>b<span>c</span></div>
</body></html>
JavaScript
JavaScript
var body = document.body;
var textContent = body.textContent || body.innerText;
console.log(textContent); // a bc
你需要检查 textContent
因为我们的好朋友IE使用 innerText
而不是。
You need to check for textContent
because our good friend IE uses innerText
instead.
如果你有一个像这样的库,那就容易多了 jQuery ,即 $('body')。text()
。
It is much easier if you have a library such as jQuery, i.e. $('body').text()
.
此外,它可以在服务器端实现,例如 strip_tags()
。但是,如果您只想要 body
元素,则需要使用DOM解析器(例如DOMDocument 。
Also, it can be achieved on the server side, such as strip_tags()
in PHP. However, if you only wanted the body
element, you'd need to drill down to it using a DOM parser such as DOMDocument.
这篇关于如何从HTML文档中仅获取文本(无标记)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!