![](/img/trans.png)
[英]How to find all the posts with a specific tag in Ghost and iterate over them?
[英]Iterate over the childred of a tag
我有這個:
var thead = document.getElementById("my_table").getElementsByTagName("thead")[0];
// =>
<thead>
<tr>
<th ....
<th ....
<th ....
現在我如何通過索引選擇或找到“ th
”? thead.children[0]
返回整個“tr”, thead.children[0][0]
未定義。 我怎樣才能到達“th”節點?
您可以使用element的children
屬性來訪問元素的子元素:
var thead = document.getElementById("my_table").getElementsByTagName("thead")[0].children[0].children;
意思是
在
my_table
找到thead
標記,獲取它的第一my_table
(tr
)並返回其子tr
的數組
現在,您將能夠遍歷它們:
for (var i = 0; i < thead.length; i++)
{
console.log(thead[i]);
}
更方便的方法是使用querySelectorAll
和CSS選擇器:
var thead = document.querySelectorAll('#my_table > thead > tr > th');
for (var i = 0; i < thead.length; i++)
{
console.log(thead[i]);
}
這是工作的JSFiddle演示 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.