我正在處理一個腳本,它通過節點執行並訪問網站的html, 其他所有工作都很完美,但是組織的一小部分腳本和填充數組不執行:
var j = 0;
var headArr = [];
$('tbody> tr > th').each(function(index) {
//^accesses elements from a loaded website^
var head = $(this).text().trim();
//^gets data from inside each 'th' tag^
headArr[j]= head;
if(headArr[j]===''){
headArr[j]=headArr[j-1]+'-';
}
//^fills an array with this data, and copies last data if this 'th' empty^
if($(this).hasClass('rowspan')){
console.log('if fired');
for(u=1; u<this.rowspan; u++){
j++;
headArr[j]=headArr[j-1]+'-';
console.log('loop fired');
}
}
//^this block is the issue^
j++;
});
第二個「如果」如果我註釋掉以外的所有的console.log塊甚至不火,我是新來的jQuery和.each
功能,而是從我的理解是:$(this)
應該是當前的'th'標記,並且在這種情況下.hasClass
應該返回true, here's the html I'm scraping。 絕對是'if'語句。
任何反饋或解決方法將不勝感激。
-Aran
試着調試你的代碼 –
'rowspan'不是你的className。這是一個'.attr()'。 – PHPglue
使用jQuery,它也有助於粘貼HTML,以便查看您的問題的人可以重新創建DOM遍歷。 –