3 回答

TA貢獻1906條經驗 獲得超3個贊
請參閱下面的您正在尋找的要求
let allLinks = document.querySelectorAll("div.ABCD li > a")
let allLinksarray = Array.from(allLinks).map(linkElem => linkElem.innerHTML)
console.log(allLinksarray)
<div class="ABCD">
<h2 class="AB">Tags</h2>
<ul>
<li><a href="/example">cat</a></li>
<li><a href="/example2">dog</a></li>
<li><a href="/example3">cow</a></li>
<li><a href="/example4">hen</a></li>
<li><a href="/example5">elephant</a></li>
</ul>
</div>

TA貢獻1942條經驗 獲得超3個贊
假設您是初學者(對不起,如果我在這里判斷錯誤),這里有一個詳細的解釋:( 鏈接)
let list = document.getElementById('list').children // Add the ID 'list' to the ul element, and declare it here.
let myArray = [] // Empty array for the results to go too
for (let item in list) { // For every item in the list...
if (list[item].children) { // Take the element with the index of 'item' from the list, and find it's children (a tag)
myArray.push(list[item].children[0].innerHTML) // Add the innerHTML (text) to a new item on the array
}
}
console.log(myArray) // Log the Array
這里還有很多其他的解決方案也可以

TA貢獻1841條經驗 獲得超3個贊
使用 jQUERY
const li = $('.ABCD').find('li > a');
const resultArray = Array.from(li).map(a => a.innerHTML)
console.log(resultArray);
添加回答
舉報