Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

jquery - Retrieving an array of link titles matching class

How would I go about creating an array containing each link title from the following? (Link 1, Link 2, Link 3) - I'm trying to pass this data through a load() command so it then pre-caches the results to reduce load time if the user decides to click on one of the links.. (each link calls to an api fetching data related to the title)

<ul id="links">
    <li><a href="#" title="Link 1">Link 1</li>
    <li><a href="#" title="Link 2">Link 2</li>
    <li><a href="#" title="Link 3">Link 2</li>
</ul>

My idea:

var elems = document.getElementsByTagName("#links li a"); 
var arr = jQuery.makeArray(elems);
??

But i'm not sure how to get just the title attr() of each link and send it over as an array

Thank you :D

Perhaps it's better to use each() and then have the load() within that, so it makes independent load calls recursively?

Something like this instead:

    $("#similar-artists li a").each(function() { 
                    alert("alert");  // load();
}); 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use .map() to get the array of titles, for example:

var arr = $("#links a").map(function() { return this.title; }).get();

You can give it a try here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...