我在HTML中有下表,并希望以编程方式从表中的每一行中提取call_id属性。
我从编辑器中插入代码和简化视图,以便您可以看到我感兴趣的内容:
我执行以下调用来获取整个表,然后希望使用此对象从以下位置检索数据:
var recordingTable = parent.recordingsFrame.document.getElementById("recordingTable");
对于这个解决方案,我的目标是纯粹的javascript,而不是使用外部库的帮助。
<table cellspacing="1" id="recordingTable"><input id="" name="" type="hidden" value="recordingTable">
<thead class="callView">
<tr>
<th>
<translate>STATE</translate>
</th>
<th>
<translate>Call ID</translate>
</th>
<th>
<translate>Text Type</translate>
</th>
<th>
<translate>Start time</translate>
</th>
<th>
<translate>Call duration</translate>
</th>
<th>
<translate>Radio/Unit ID</translate>
</th>
<th>
<translate>Participant name</translate>
</th>
<th>
<translate>Tag</translate>
</th>
<th>
<translate>Note</translate>
</th>
<th>
<translate></translate>
</th>
</tr>
</thead>
<tbody class="callView">
<tr active="0" archivecount="0" callprovidertype="16" call_id="5" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="1" screenreccount="0" screensempty="true" issms="false" istext="true" style="background-color: rgb(208, 230, 248);">
<td class="spannedCell" style="color: rgb(18, 159, 221);">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span>5</span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span></span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span>2013-08-06 19:11:24</span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span>00:02:00</span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);">
<div>
<div>5010</div>
<div>5010 (Caller)</div>
</div>
</td>
<td class="spannedCell" style="color: rgb(18, 159, 221);">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span></span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);">
<div title="00 D4 14 86 7E CA 3B 7D 61 20">00 D4 14 8...</div>
</td>
<td class="tableColumn" style="color: rgb(18, 159, 221);"></td>
</tr>
<tr active="0" archivecount="0" callprovidertype="16" call_id="4" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="2" screenreccount="0" screensempty="true" issms="false" istext="true">
<td class="spannedCell">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell"><span>4</span></td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell"><span>2013-08-06 19:11:17</span></td>
<td class="spannedCell"><span>00:02:00</span></td>
<td class="spannedCell">
<div>
<div>8882103</div>
<div>5006 (Caller)</div>
</div>
</td>
<td class="spannedCell">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell">
<div title="00 D4 0E 96 7E DF 9E FC 80 F0">00 D4 0E 9...</div>
</td>
<td class="tableColumn"></td>
</tr>
<tr active="0" archivecount="0" callprovidertype="16" call_id="3" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="1" screenreccount="0" screensempty="true" issms="false" istext="true">
<td class="spannedCell">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell"><span>3</span></td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell"><span>2013-08-06 19:11:11</span></td>
<td class="spannedCell"><span>00:02:00</span></td>
<td class="spannedCell">
<div>
<div>5010</div>
<div>5010 (Caller)</div>
</div>
</td>
<td class="spannedCell">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell">
<div title="00 D3 F1 16 7E 3D D5 87 80 60">00 D3 F1 1...</div>
</td>
<td class="tableColumn"></td>
</tr>
<tr active="0" archivecount="0" callprovidertype="16" call_id="2" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="1" screenreccount="0" screensempty="true" issms="false" istext="true">
<td class="spannedCell">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell"><span>2</span></td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell"><span>2013-08-06 19:08:47</span></td>
<td class="spannedCell"><span>00:02:00</span></td>
<td class="spannedCell">
<div>
<div>8882107</div>
<div>5014 (Caller)</div>
</div>
</td>
<td class="spannedCell">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell">
<div title="20 D4 12 BE 7E 93 E9 4C E1 20">20 D4 12 B...</div>
</td>
<td class="tableColumn"></td>
</tr>
<tr active="0" archivecount="0" callprovidertype="16" call_id="1" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="1" screenreccount="0" screensempty="true" issms="false" istext="true">
<td class="spannedCell">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell"><span>1</span></td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell"><span>2013-08-06 19:08:47</span></td>
<td class="spannedCell"><span>00:02:00</span></td>
<td class="spannedCell">
<div>
<div>8882105</div>
<div>5010 (Caller)</div>
</div>
</td>
<td class="spannedCell">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell">
<div title="20 D4 08 66 7E 82 A9 4C E1 20">20 D4 08 6...</div>
</td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
这是一个快速的代码段,它包含了表格中的所有call_id值:
var rows = document.querySelectorAll('[call_id]')
var callIds = Array.prototype.map.call(rows, function(tr){
return tr.getAttribute('call_id')
})
console.log(callIds)
代码说明:
document.querySelectorAll()
向DOM查询与选择器匹配的所有元素[call_id]
,它获取的属性匹配call_id="<any>"
的任何元素document.querySelectorAll()
的返回值不是带有map()
方法的普通数组,我使用Array的原型来调用类方法映射,将结果作为this
传递值map()
返回一个新的数组,其中包含回调返回的任何内容,在本例中为call_id