簡體   English   中英

使用正則表達式對頁面上的所有鏈接進行JavaScript

[英]JavaScript all links on a page with regex

我正在嘗試提取具有以下標記的網頁上的所有鏈接:

<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與地圖返回(被截斷)

    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': 
...

較新的瀏覽器:

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM