非常简单,我需要获取所有第一种类型的元素文本:
<html>
<body>
<table>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
<tr>
<td>Line #1</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Line #2</td>
<td>SLD</td>
<td>68</td>
</tr>
<tr>
<td>Line #3</td>
<td>MDK</td>
<td>68</td>
</tr>
</table>
<script type="text/javascript">
console.log(document.querySelectorAll('table tr td:first-of-type'));
</script>
</body>
</html>
我希望能够获取所有表 tr td:first-of-type insidetext...这里需要进行一些小调整。
最佳答案
我以某种方式解决了它...反复试验...谢谢:)
<html>
<body>
<table>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<tr>
<td>Line #1</td>
<td>MKD</td>
<td>68</td>
</tr>
<tr>
<td>Line #2</td>
<td>SLD</td>
<td>32</td>
</tr>
<tr>
<td>Line #3</td>
<td>HAD</td>
<td>53</td>
</tr>
<tr>
<td>Line #4</td>
<td>LRD</td>
<td>49</td>
</tr>
</table>
<script type="text/javascript">
var theFirstChilds = document.querySelectorAll('table tr td:first-of-type');
var i;
for (i = 0; i < theFirstChilds.length; ++i) {
console.log(theFirstChilds[i].innerText);
}
</script>
</body>
</html>
关于javascript - 从表中获取所有第一个子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134069/