我正在尝试提取具有以下标记的网页上的所有链接:
<a href="/item/0/100">0</a>
<a href="/item/1/100">2</a>
<a href="/item/2/100">3</a>
<a href="/item/3/100">4</a>
<a href="/item/4/100">5</a>
基本上返回所有/item...
路径。我有包含这个的dom对象。知道怎么做吗?
谢谢!
编辑:使用jQuery和Map返回(截断)
http:undefined
{ '0': '/item/200/13/0',
'1': '/item/200/1/0',
'2': '/item/200/4/0',
'3': '/item/200/5/0',
'4': '/item/200/11/0',
length: 4,
prevObject:
{ '0':
{ _ownerDocument: [Object],
_childNodes: [Object],
_attributes: [Object],
_nodeName: 'a',
_childrenList: null,
_version: 3,
_nodeValue: null,
_parentNode: [Object],
_readonly: false,
_tagName: 'a',
_created: true,
_attached: true,
_attachedToDocument: true },
'1':
...
答案 0 :(得分:7)
较新的浏览器:
var links = document.querySelectorAll('a[href^="/item/"]');
旧浏览器:
var links = [];
var elements = document.getElementsByTagName('a');
for (var i = 0; i < elements.length; i++) {
var a = elements[i];
if (a.getAttribute('href').indexOf('/item/') === 0) {
links.push(a);
}
}