获取网站的所有 href 属性 [英] get all the href attributes of a web site

查看:38
本文介绍了获取网站的所有 href 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我一种使用 javascript 获取网站中所有 href 属性(链接)的方法吗?如果您能给我一个代码示例,我将不胜感激.

can anybody tell me a way to get all the href attributes(links) in a web site using javascript?if you could give me a code example, i will be most thankful.

推荐答案

您可以使用 document.links 获取锚点,然后循环抓取href,如下所示:

You can use document.links to get the anchors, then just loop through grabbing the href, like this:

var arr = [], l = document.links;
for(var i=0; i<l.length; i++) {
  arr.push(l[i].href);
}
//arr is now an array of all the href attributes from the anchors in the page

你可以在这里测试,你可以在.push() 如果需要,可以调用数组,但这就是抓取链接和循环的概念.

You can test it out here, you could filter it more before the .push() call on the array if you wanted, but that's the concept for grabbing the links and looping through.

这篇关于获取网站的所有 href 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆