Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

javascript - How can I get the text only (no tags) from a HTML document?

I have a HTML page, and I want the text only (all text nodes).

Example HTML

<span>hello <strong>sir</strong></span>

Desired Output

hello sir
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming you only want children of body element...

Example HTML

<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

var body = document.body;
var textContent = body.textContent || body.innerText;

console.log(textContent);  //   a bc

You need to check for textContent because our good friend IE uses innerText instead.

It is much easier if you have a library such as jQuery, i.e. $('body').text().

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...