[問題] jQuery 一直出現莫名其妙的錯誤

作者: bruce0428 (bruce)   2019-11-26 10:13:18
for(var p=0; p<(list.childNodes.length-1);p++){
x=list.childNodes[p].childNodes[4].innerHTML;
y=list.childNodes[p+1].childNodes[4].innerHTML;
c=list.childNodes[p];
// console.log(c)
var x1=x.split(":")[0];
var x2=x.split(":")[1];
var y1=y.split(":")[0];
var y2=y.split(":")[1];
if(Number(x1)>Number(y1)||(Number(x1)==Number(y1)&&Number(x2)>Number(y2))){
shouldSwitch = true;
break;
}
}
測試時
一直出現以下錯誤
Uncaught TypeError: Cannot read property 'innerHTML' of undefined
at Object.success (tra.js:354)
at fire (jquery-1.12.4.js:3232)
at Object.fireWith [as resolveWith] (jquery-1.12.4.js:3362)
at done (jquery-1.12.4.js:9840)
at XMLHttpRequest.callback (jquery-1.12.4.js:10311)
這該怎麼解決
作者: LZN (秋)   2019-11-26 11:02:00
把childNode都印出來檢查吧
作者: LPH66 (-6.2598534e+18f)   2019-11-26 15:30:00
你的 HTML 長怎樣?
作者: bakedgrass (蒙古烤小草)   2019-11-27 01:03:00
console.log印出你的childNode和其parent node,看有沒有抓對你要的DOM
作者: laechan (揮淚斬馬雲)   2019-11-30 00:11:00
為啥p<(list... 那邊要括號就一般code的角度, y=list.childNodes[p+1]這裡會出問題喔沒,看錯,沒事
作者: joedenkidd (優質的藍色射手)   2019-12-02 08:25:00
個人覺得 p+1那行有問題
作者: LPH66 (-6.2598534e+18f)   2019-12-03 23:20:00
同問貼 HTML 出來, 只看這裡我會猜問題在 childNodes[4]
作者: wotupset (wotupset)   2019-12-04 09:01:00
應該是childNodes抓錯了 所以找不到innerHTML屬性
作者: laechan (揮淚斬馬雲)   2019-12-08 01:09:00
.each +1, 用慣了就回不去了:p

Links booklink

Contact Us: admin [ a t ] ucptt.com