今天,我想记录一个函数,这是后端返给我一个html页面,把页面中的标题、链接等自己需要的一些东西摘出来,组成一个数组然后循环出来。
getDocumentInfo(PageSize) {
let _this = this;
var html = $(unescape(this.data));
$(html.find("tbody")[1]).children().each(function (i) {
if (i > 0) {
var newsObj = {};
var tds = $(this).children();
var title = tds.eq(1);
var atitle = title.attr('title');
var catagory = tds.eq(2);
newsObj.title =
"【" + catagory.html() + "】" + atitle;
newsObj.href =
"链接" +
title.find("a").attr("href");
newsObj.datetime = tds.eq(4).html();
newsObj.attinfo = "";
var attachment = tds.eq(3);
var ps = attachment.find("p");
if (ps.length > 0) {
var _length = ps.length / 2;
newsObj.attinfo =
"附件<span style='color:red'>(" +
_length +
"个)</span>";
newsObj.attList = [];
var ps = attachment.find("p");
var $this = $(this);
ps.each(function () {
var a = $(this).find("a");
if (a.length > 0) {
newsObj.attList.push({
name: a.html(),
href:
"链接" +
a.attr("href"),
});
}
});
}
_this.gwlist.push(newsObj);
}
});
console.log(_this.gwlist)
},