前端学习之js--出现并列名次的排序

如图是实现的效果

1、实时刷新页面,显示数据库中不断改变的投票数;

2、如果投票数相同,则名次相同。如图:

第一个功能用了框架,是在前端写setInterval方法

第二个功能遇到了一点问题:具体说一下

思路:先根据票数做降序排序,再比较一个节目A的票数与下一个节目B的票数,如果相等,则将B的图片设置成A;

代码:

setInterval(function myrefresh()
{
Artery.get("jqGrid18f86").reload();
countList= Artery.get("jqGrid18f86").getValuesArray("jqColumnStringace8e");
nameList = Artery.get("jqGrid18f86").getValuesArray("jqColumnString54dea");
var name = document.getElementsByClassName("actName");
for (var j = 0; j < nameList.length; j++) {
name[j].innerHTML = nameList[j].text;//设置节目名
}
var count = document.getElementsByClassName("actCount");
var num= document.getElementsByClassName("num");
for (var j = 0; j < countList.length; j++) {
count[j].innerHTML = countList[j].text;//设置票数
if(j<countList.length){
if(countList[j].text===countList[j+1].text){
num[j+1].style.backgroundImage=num[j].style.backgroundImage;
}
}
}
},500);

结果运行时报错Cannot read property 'text' of undefined

经过检查,没发现语法问题

于是尝试换一种写法,将

if(j<countList.length){
if(countList[j].text===countList[j+1].text){
num[j+1].style.backgroundImage=num[j].style.backgroundImage;
}

改成

if(j>0){
if(countList[j-1].text===countList[j].text){
num[j].style.backgroundImage=num[j-1].style.backgroundImage;
}

没再报错。

我认为,可能是浏览器在做for循环遍历数组时,只能获得当前对象及它之前的对象,当前对象的下一个对象它不能预先获得(虽然调试时没问题,因此这是我的猜想)。

posted @ 2019-01-16 20:13  小纸盒  阅读(1669)  评论(0编辑  收藏  举报