2017-10-05 5 views
0

Lets say I havejavascript analyser le texte de <a href> links

<a href="/example1">ThisTextChanges</a> 
<a href="/example2">ThisTextChanges</a> 
<a href="/example3">ThisTextChanges</a> 
<a href="/example4">ThisTextChanges</a> 

I want to iterate through these and get the "ThisTextChanges" which are some numbers that changes, most accurately timers.

How can i achieve that? jquery is fine. They are inside a div with id "main_container". I need to put the text in a var so the href is importanto to know which var i use for each one.

+0

Use ['.contains(myVar)'](https://api.jquery.com/contains-selector/) in your selector for 'a' tags. –

+0

do you mean if '123 'produira var nommé' example1' avec la valeur '123', et ainsi de suite? –

Répondre

0

You can just add condition in the a selector as follows:

var array = []; 
$('#main_container a[href="/example2"]').each(function(){ 
    array.push($(this).html()); 
}); 
console.log(array); 
0

You can iterate and store them in an Array

var arr = []; 

$("a").each(function(){ 

    arr.push($(this).text()); 
    console.log(arr); 

}); 
0

you can achieve that in may ways. this example using for loop.

var main_container = document.getElementById("main_container"); 
var items = main_container.getElementsByTagName("a"); 
for (var i = 0; i < items.length; ++i) { 
     // do something..... 
} 
1

Lets break the task down into several steps:

  • Get a handle to all of our links (document.querySelectorAll)
  • learn how to get the current text of an a tag (childNode[0].nodeValue)

  • put it all together (Array.from, Array.map)

Get a handle to all of our links:

we will use document.querySelectorAll to get list of all nodes that match our selector. here I'm just going to use the selector a, but you probably have a class that specifies these links vs other links on the page:

var links = document.querySelectorAll('a'); 

Get the text of a link

This one is a bit more complicated. There are several ways to do this, but one of the more efficient ways is to loop through the child nodes (which will mostly be text nodes), and append the node.nodeValue for each one. We could probably get away with just using the nodeValue of the first child, but instead we'll build a function to loop through and append each.

function getText(link){ 
    var text = ""; 
    for (var i = 0; i < link.childNodes.length; i++){ 
     var n = link.childNodes[i]; 
     if (n && n.nodeValue){ 
      text += n.nodeValue; 
     } 
    } 
    return text; 
} 

Put it all together

To put it all together we will use Array.map to turn each link in our list into the text inside it. This will leave us with an array of strings. However in order to be able to pass it to Array.map we will have to have an array, and document.querySelectorAll returns a NodeList instead. So to convert it over we will use Array.from to turn our NodeList into an array.

function getText(link){ 
 
    var text = ""; 
 
    for (var i = 0; i < link.childNodes.length; i++){ 
 
     var n = link.childNodes[i]; 
 
     if (n && n.nodeValue){ 
 
      text += n.nodeValue; 
 
     } 
 
    } 
 
    return text; 
 
} 
 

 
var linkTexts = Array.from(document.querySelectorAll('a')) 
 
       .map(getText); 
 

 
console.log(linkTexts);
<a href="1">this is text</a> 
 
<a href="2">this is some more text</a>

0

var array = []; 
 
$('#main_container a').each(function(){ 
 
    array.push($(this).html()); 
 
}); 
 
console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="main_container"> 
 
     <a href="/example1">ThisTextChanges 1</a> 
 
     <a href="/example2">ThisTextChanges 2</a> 
 
     <a href="/example3">ThisTextChanges 3</a> 
 
     <a href="/example4">ThisTextChanges 4</a> 
 
    </div>

+0

Cela semble simple et c'est tout. Comment puis-je le faire afin qu'il recherche une URL spécifique? par exemple seulement rassembler le texte de '/ example2'? Parce que c'est la seule façon dont je sais que l'analyse va dans chaque var, c'est par l'URL associée. –

+0

Je suis heureux de pouvoir vous aider, s'il vous plaît accepter si cela résout votre problème ou vous aider! – Kamal

+0

J'ai besoin d'un peu plus d'aide, j'essayais de casser la ligne et posté avant que j'aie fini d'écrire. –

0

Please try:

$('#main_container > a[href]').each(function() { 
    var tes = $(this).attr('href').substring(1); 
    window[tes] = $(this).text(); 
}); 

<a href="/example1">123</a> will produce var named example1 with value 123, and so on.