如何解决查询选择器如何跳过一个td并将其用作部分名称
我以这种方式构造了html表:
<table class="generaltable boxaligncenter" id="outlinetable" cellpadding="1">
<thead>
<tr>
<th class="header c0">Activity</th>
</tr>
</thead>
<tbody id="yui_3_17_2_1_1599471686149_651">
<tr class="">
<td class="activity cell c0">
<a class="dimmed" href="http://somesite.com.np/mod/questionnaire/view.php?id=304">Survey on student familiarity with the online learning
platform</a
>
</td>
</tr>
<tr class="">
<td class="activity cell c0">
<a
href="http://somesite.com.np/mod/assign/view.php?id=307"
id="yui_3_17_2_1_1599471686149_724"
>update your profile</a
>
</td>
</tr>
<tr class="">
<td class="activity cell c0">
<a href="http://somesite.com.np/mod/resource/view.php?id=352"
>COURSE MANUAL</a
>
</td>
</tr>
<tr class="section" id="yui_3_17_2_1_1599471686149_685">
<td
class="cell c0 lastcol"
colspan="4"
id="yui_3_17_2_1_1599471686149_684"
>
<h3 id="yui_3_17_2_1_1599471686149_687">Week 9</h3>
</td>
</tr>
<tr class="" id="yui_3_17_2_1_1599471686149_680">
<td class="activity cell c0" id="yui_3_17_2_1_1599471686149_679">
<a
href="http://somesite.com.np/mod/page/view.php?id=568"
id="yui_3_17_2_1_1599471686149_682"
>Click me to get the welcome badge for week 9.</a
>
</td>
</tr>
<tr class="section" id="yui_3_17_2_1_1599471686149_698">
<td
class="cell c0 lastcol"
colspan="4"
id="yui_3_17_2_1_1599471686149_697"
>
<h3 id="yui_3_17_2_1_1599471686149_706">WEEK 9: ACTIVITIES</h3>
</td>
</tr>
<tr class="" id="yui_3_17_2_1_1599471686149_702">
<td class="activity cell c0" id="yui_3_17_2_1_1599471686149_701">
<a
href="http://somesite.com.np/mod/url/view.php?id=540"
id="yui_3_17_2_1_1599471686149_700"
>Keyboards for Disabled People</a
>
</td>
</tr>
<tr class="" id="yui_3_17_2_1_1599471686149_657">
<td class="activity cell c0" id="yui_3_17_2_1_1599471686149_656">
<a
href="http://somesite.com.np/mod/url/view.php?id=541"
id="yui_3_17_2_1_1599471686149_659"
>Mobile interaction</a
>
</td>
</tr>
<tr class="section">
<td class="cell c0 lastcol" colspan="4">
<h3>Week 8</h3>
</td>
</tr>
<tr class="">
<td class="activity cell c0">
<a href="http://somesite.com.np/mod/page/view.php?id=531"
>Click me to get the welcome badge for week 8.</a
>
</td>
</tr>
<tr class="section">
<td class="cell c0 lastcol" colspan="4">
<h3>Week 8: Activities</h3>
</td>
</tr>
<tr class="">
<td class="activity cell c0">
<a href="http://somesite.com.np/mod/hvp/view.php?id=519"
>You Suck at Form Design ((Probably))</a
>
</td>
</tr>
</tbody>
</table>
我想选择href
和td
中class="activity cell c0"
内的文本,并使用h3
内class="cell c0 lastcol"
标记内的类文本
我能够在循环中获取href
和section
,但是如何将其推入数组中?
我想将值推入newList
内。
var t2 = document.querySelectorAll('.generaltable.boxaligncenter tbody tr');
var newList = [];
var section = null;
for (var i = 0; i < t2.length; i++) {
var href = t2[i].querySelector('td.c0 > a');
if(href===null){
section = t2[i].querySelector('td.c0 > h3').innerText;
console.log(`section ${section}`);
}
newList.push( {
Activity: href.innerText,URL: href.getAttribute('href'),id: href.getAttribute('href').substring(href.getAttribute('href').lastIndexOf('=') + 1),Section: section
});
}
我试图将其推送到一个数组,但是有错误:
Uncaught TypeError: Cannot read property 'innerText' of null
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。