【发布时间】:2011-05-03 10:12:48
【问题描述】:
我正在尝试从通过 ajax 下载的正文中提取文本。
我无法将此新正文注入 iframe 或我的元素之一,因为其中的脚本可能会破坏我的页面。
我希望为我分叉的是:
$.ajax({ type: "GET",
dataType: "text", /* this will avoid evaluating scripts */
url: href,
success: function (data) {
var body = data.split('<body').pop().split('</body>')[0];
if (body) {
body = '<body' + body + '</body>';
var pageText = $(body).find("style").remove().end()
.find("script").remove().end()
.find("noscript").remove().end()
.text().replace(/\s{2,}/gi, " ").toLowerCase();
if (pageText.length > 0)
console.log(pageText);
});
我尝试将下载的正文放入 DIV 元素,因为 jQuery 忽略了 BODY,将 find.remove 替换为 detach 但没有多大成功。
有没有标准的解决方案?
谢谢
【问题讨论】:
标签: jquery html ajax dom html-parsing