老师的要求中还有一项,就是可以对结果列表进行筛选,根据年份进行选择,在做这一部分的时候我发现我以前写的代码由于不够好,现在加上这一功能就要修改不少代码,因此我打算再加上一个方法,让这个方法更加的适用于再结果列表中筛选,
主要的方法的代码如下:
function changedata(){
var chdanum=0;
var showdata=new Array(0);
var vl=document.getElementById("resselect");
var index = vl.selectedIndex;
var chva=vl.options[index].value;
document.getElementById("restable").remove();
var str="<table id='restable'>"+"<tr><td width='35%'>标题</td><td width='35%'>作者</td><td width='15%'>时间</td><td width='15%'>链接</td></tr>";
if(chva=="近两年"){
for(i in alldata){
if(alldata[i].time.substring(0,4)!="2018"){
chdanum=chdanum+1;
var onedata={};
//str=str+"<tr><td>"+alldata[i].title+"</td><td>"+alldata[i].zuozhe+"</td><td>"+alldata[i].time+"</td><td><a target='_blank' href='"+alldata[i].lianjie+"'>"+"点此阅读文章"+"</a></td></tr>";
onedata["title"]=alldata[i].title;
onedata["zuozhe"]=alldata[i].zuozhe;
onedata["time"]=alldata[i].time;
onedata["lianjie"]=alldata[i].lianjie;
showdata.push(onedata);
}
}
}
else if(chva=="去年"){
for(i in alldata){
if(alldata[i].time.substring(0,4)=="2020"){
chdanum=chdanum+1;
var onedata={};
//str=str+"<tr><td>"+alldata[i].title+"</td><td>"+alldata[i].zuozhe+"</td><td>"+alldata[i].time+"</td><td><a target='_blank' href='"+alldata[i].lianjie+"'>"+"点此阅读文章"+"</a></td></tr>";
onedata["title"]=alldata[i].title;
onedata["zuozhe"]=alldata[i].zuozhe;
onedata["time"]=alldata[i].time;
onedata["lianjie"]=alldata[i].lianjie;
showdata.push(onedata);
}
}
}
else if(chva=="近三年"){
for(i in alldata){
chdanum=chdanum+1;
var onedata={};
//str=str+"<tr><td>"+alldata[i].title+"</td><td>"+alldata[i].zuozhe+"</td><td>"+alldata[i].time+"</td><td><a target='_blank' href='"+alldata[i].lianjie+"'>"+"点此阅读文章"+"</a></td></tr>";
onedata["title"]=alldata[i].title;
onedata["zuozhe"]=alldata[i].zuozhe;
onedata["time"]=alldata[i].time;
onedata["lianjie"]=alldata[i].lianjie;
showdata.push(onedata);
}
}
qjpagenum=parseInt(showdata.length/30)+1;
pagealldata=showdata;
var maxnum=0;
if((parseInt(qjnowpage)+1)*30<showdata.length)maxnum=(parseInt(qjnowpage)+parseInt(1))*30;
else maxnum=showdata.length;
for(i=parseInt(qjnowpage)*30;i<maxnum;i++){
str=str+"<tr><td>"+showdata[i].title+"</td><td>"+showdata[i].zuozhe+"</td><td>"+showdata[i].time+"</td><td><a target='_blank' href='"+showdata[i].lianjie+"'>"+"点此阅读文章"+"</a></td></tr>";
}
var div=document.getElementById("reslunwendiv");
div.innerHTML = str+"</table>";
var formdiv=document.getElementById("tjdiv");
document.getElementById("changeresdiv").remove();
var str="<select id='resselect' onchange='changedata();setnowpage();fenye()'><option value='选择年份'>选择年份</option><option value='近三年'>近三年</option><option value='近两年'>近两年</option><option value='去年'>去年</option></select>";
str=str+"<h2 align='center' id='totalnumh'>一共"+chdanum+"条</h2>"
var div1 = document.createElement("div");
div1.id="changeresdiv";
div1.innerHTML = str;
formdiv.appendChild(div1);
}
然后这个方法中还带着分页的方法,这里就不贴出来了。