2017-10-17 62 views
0

我正在處理一個腳本,它通過節點執行並訪問網站的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

+0

試着調試你的代碼 –

+0

'rowspan'不是你的className。這是一個'.attr()'。 – PHPglue

+0

使用jQuery,它也有助於粘貼HTML,以便查看您的問題的人可以重新創建DOM遍歷。 –

回答

3

"rowspan"是一個屬性,而不是在class HTML的鏈接的圖像。您可以使用.is()與屬性選擇器來獲得一個布爾值

if ($(this).is("[rowspan]")) 
+0

果然你的條件解僱了,謝謝閣下。 –

0

你可以這樣做:

if($(this).attr('rowspan'))