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);
添加回答
举报
