[英]Javascript to get text node of multiple tds using tr class
this question is somewhat related to the first question I posted here: Javascript to get text node of td using tr class I have the following codes below: 这个问题与我在这里发布的第一个问题有些相关: 使用tr类获取td的文本节点的Java语言我有以下代码:
<table>
...
[Multiple div lines ommited for brevity]
<tr class="bibItemsEntry">
<td width="31%" ><!-- field 1 --> Archives, Thesis Col. Graduate, 12F (Mezz.) Henry Sy Sr. Hall
</td>
<td width="46%" ><!-- field C --> <a href="/search~S1?/cCDTG006829/ccdtg006829/-3,-1,,E/browse">CDTG006829</a> <!-- field v --><!-- field # --> <!-- field ! --> ROOM USE ONLY</td>
<td width="23%" ><!-- field % --> CHECK SHELF </td></tr>
</table>
<p id="demo">Test</p>
[multiple divs <tr>s <td>s omitted for brevity]
....
<tr class="bibItemsEntry">
<td width="31%" ><!-- field 1 --> Shelf 194, 10F Mezzanine (Filipiniana), Henry Sy Sr. Hall
</td>
<td width="46%" ><!-- field C --> <a href="/search~S1?/cPS3568.O5333+A6+2016/cps+3568+o5333+a6+2016/-3,-1,,E/browse">PS3568.O5333 A6 2016</a> <!-- field v --><!-- field # --> <!-- field ! --> ROOM USE ONLY</td>
<td width="23%" ><!-- field % --> LIB USE ONLY </td></tr>
</table>
<p id="demo">Test</p>
What I wanted to do is to use javascript and get the first value of each occurence of the text node of <td>
under <tr class="bibItemsEntry">
and and put into p id=demo (That is into their respective s) . 我想做的是使用javascript,并在
<tr class="bibItemsEntry">
下获取<tr class="bibItemsEntry">
<td>
文本节点的每次出现的第一个值,并将其放入p id = demo(即分别放入它们的s中) 。 I'm trying to put the value into <p id="demo">
. 我正在尝试将值放入
<p id="demo">
。 I'm trying out the below javascript code: 我正在尝试以下javascript代码:
var x = document.getElementsByClassName("bibItemsEntry");
document.getElementById("demo").innerHTML = x[0].innerHTML;
[benvc][1] answered the below code, thanks to him/her:
const td = document.querySelector('.bibItemsEntry td:first-child');
const p = document.getElementById('demo');
p.innerHTML = td.textContent;
it only gets the first iteration which is true enough, I was not able to put that in my first question... Thanks in advance!
它只会得到足够真实的第一个迭代,我无法在第一个问题中提到它……预先感谢!
You need to loop through the x
result like : 您需要像这样遍历
x
结果:
var x = document.getElementsByClassName("bibItemsEntry"); [].forEach.call(x, function(item) { var demo = item.querySelector('.demo'); var firstTd = item.querySelector('td:first-child'); demo.textContent = firstTd.textContent; });
<table id="codexpl" border="1"> <tr> <th>#</th> <th>Text</th> <th>Demo</th> </tr> <tr class="bibItemsEntry"> <td width="31%"> <!-- field 1 --> Archives, Thesis Col. Graduate, 12F (Mezz.) Henry Sy Sr. Hall </td> <td width="46%"> <!-- field C --> <a href="/search~S1?/cCDTG006829/ccdtg006829/-3,-1,,E/browse">CDTG006829</a> <!-- field v --> <!-- field # --> <!-- field ! --> ROOM USE ONLY</td> <td> <p class="demo"></p> </td> </tr> <tr class="bibItemsEntry"> <td width="31%"> <!-- field 1 --> Shelf 194, 10F Mezzanine (Filipiniana), Henry Sy Sr. Hall </td> <td width="46%"> <!-- field C --> <a href="/search~S1?/cPS3568.O5333+A6+2016/cps+3568+o5333+a6+2016/-3,-1,,E/browse">PS3568.O5333 A6 2016</a> <!-- field v --> <!-- field # --> <!-- field ! --> ROOM USE ONLY</td> <td> <p class="demo"></p> </td> </tr> </table> <div id="demo"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.