亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 java 腳本或 jquery 提取 <li> 標記內的文本

使用 java 腳本或 jquery 提取 <li> 標記內的文本

精慕HU 2022-10-13 09:47:46
var array1[] ;//i want to store text inside href in a array like this// [cat,dog,cow,hen,elephant] , how can I do that?<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>我想使用 javascript 讓 cat,dog,cow,hen,elephant 排列動物 []。我怎樣才能把它們放到一個數組中?請幫忙
查看完整描述

3 回答

?
一只名叫tom的貓

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>


查看完整回答
反對 回復 2022-10-13
?
手掌心

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

這里還有很多其他的解決方案也可以


查看完整回答
反對 回復 2022-10-13
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

使用 jQUERY


const li  = $('.ABCD').find('li > a');

const resultArray = Array.from(li).map(a => a.innerHTML)

console.log(resultArray);


查看完整回答
反對 回復 2022-10-13
  • 3 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號