This is a challenge for any Javascript/jQuery ninjas out there:
What is the best way (using aforementioned languages), to find all of the links in an HTML document and return them all?
In other words, a function that like this - findLinks(document.html.innerHTML) that would return all links found in that HTML.
Thanks,
DLiKS
好吧,你可以摆弄一个厚实的图书馆(如果你最终希望做有趣的事情来操纵结果,这可能是一个好主意),但只是为了得到链接,我认为我坚持DOM 0:
document.links
To get all hrefs from existing anchor elements, you can do the following, which will return them as an array:
var links = $("a").map(function() {
return this.href;
}).get();
If you just want to grab each anchor element and do something with them thereafter, you would just need to select them:
$("a").hide(); // or whatever
I have a bookmarklet that finds all the hyperlinks and writes them to an HTML page:
javascript:ctDL=document.links;ctWI=open('','','width=400,height=300,scrollbars,resizable,menubar');ctDO=ctWI.document;ctDO.writeln('');for(ctI=0;%20ctI')}void(ctDO.close())
It's either I don't understand the question, or it's not really that much of a challenge:
function findLinks(innerHTML){
var fragment = document.createElement('div');
fragment.innerHTML = innerHTML;
var links = [];
fragment.find('a').each(function(index, element){
links.push($(element).attr('href'));
});
return links;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.