【发布时间】:2016-08-14 21:19:35
【问题描述】:
请考虑以下 HTML 内容:
I like <div>turtles, </div><b> cookies</b> and brownies.
我正在使用下面的代码来检索内容:
txt=$.root().find('*').contents().filter(function() {
if (this.type === 'text'){
varArr.push(this.data);
return this.data
}
}).text();
txt 是:海龟、饼干
如果我添加标签
<html>I like <div>turtles, </div><b> cookies</b> and brownies</html>
我得到: 我喜欢和布朗尼。海龟,饼干
我想要: 我喜欢乌龟、饼干和巧克力蛋糕。
它不会选择不在标签之间的初始文本和最后文本。有没有办法检索所有文本 - 外部和嵌套在 html 标签上?)
【问题讨论】:
-
$("html").text()呢? -
这只能提取文本,但我必须与找到的每一段文本进行交互,即使用映射或过滤功能。刚刚编辑了我的问题,提供了一个更好的例子。
标签: javascript jquery html node.js