前端學習之js--出現並列名次的排序

如圖是實現的效果前端

一、實時刷新頁面,顯示數據庫中不斷改變的投票數;數據庫

二、若是投票數相同,則名次相同。如圖:數組

第一個功能用了框架,是在前端寫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);blog

結果運行時報錯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循環遍歷數組時,只能得到當前對象及它以前的對象,當前對象的下一個對象它不能預先得到(雖然調試時沒問題,所以這是個人猜測)。

相關文章
相關標籤/搜索