2017-10-05 8 views
0

Lets say I havejavascript parse text from <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''123'이라는 이름의 var가 'example1'이라는 이름으로 생성됩니다. –

답변

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

이것은 간단 해 보입니다. 어떻게하면 특정 URL을 검색 할 수 있습니까? 예를 들어 '/ example2'의 텍스트 만 수집합니까? 각각의 var에 parse가 들어간다는 것을 알고있는 유일한 방법 이죠. url과 관련된 것입니다. –

+0

기꺼이 당신을 도울 수 있습니다. 문제가 해결되거나 도움이된다면 받아주세요. – Kamal

+0

좀 더 도움이 필요합니다. 글을 끝내기 전에 줄을 서서 게시하려고했습니다. –

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.