<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>
var array1[] ; //i want to store text inside href in a array like this // [cat,dog,cow,hen,elephant] , how can I do that?
Je veux amener un chat, un chien, une vache, une poule, un éléphant à aligner des animaux [] en utilisant javascript.
Comment puis-je les amener à un tableau? s'il vous plaît aider
7 Réponses :
<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>
var array = [];
var links = document.querySelectorAll('a');
links.forEach(link => array.push(link.text));
console.log(array);
Veuillez consulter ci-dessous l'exigence que vous recherchez
<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>
let allLinks = document.querySelectorAll("div.ABCD li > a")
let allLinksarray = Array.from(allLinks).map(linkElem => linkElem.innerHTML)
console.log(allLinksarray)
J'espère que cela vous aidera.
Vous pouvez faire quelque chose comme ça:
<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>
const lists = Array.from(document.querySelectorAll('li'));
let texts = lists.map(list => list.textContent);
console.log(texts);
Utilisation de jQUERY
const li = $('.ABCD').find('li > a');
const resultArray = Array.from(li).map(a => a.innerHTML)
console.log(resultArray);
var list = document.querySelectorAll('.ABCD a');
Array.prototype.map.call(list, function (i) {
return i.innerText
});
you may refer to mdn for further information. https://developer.mozilla.org/en-US/docs/Web/API/NodeList#Example
Quelle est votre question?
En supposant que vous soyez débutant (désolé si je ne juge pas correctement ici), voici une explication détaillée à ceci: (lien)
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
Il existe de nombreuses autres solutions ici fonctionne aussi
//variables
let myArray = [];
let list = $('.ABCD li a');
//get the list from html and puts on array
list.forEach(list => myArray.push(list.text));
//annoying alert with your list
alert(myArray);