我正在编写一个通过 Node 执行并访问网站 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之外的所有内容,第二个'if' block 也不会触发,我对jquery和.each
函数比较陌生,但根据我的理解:$(this)
应该是当前的'th'标签,在这种情况下.hasClass
应该返回true,
here's the html I'm scraping 。
绝对是“if”语句。
任何反馈或解决方法将不胜感激。
-阿兰
最佳答案
"rowspan"
是一个属性,而不是 HTML 链接图像中的 class
。您可以将 .is()
与属性选择器一起使用来获取 bool 值
if ($(this).is("[rowspan]"))
关于javascript - $(this).hasClass ('e.g' ) 不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46781717/