計算後代
重要性:5
讓我們對 <li>
進行迴圈。
for (let li of document.querySelectorAll('li')) {
...
}
在迴圈中,我們需要取得每個 li
中的文字。
我們可以從 li
的第一個子節點讀取文字,也就是文字節點
for (let li of document.querySelectorAll('li')) {
let title = li.firstChild.data;
// title is the text in <li> before any other nodes
}
然後我們可以取得後代的數量,方法是 li.getElementsByTagName('li').length
。