Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...