Javascript 获取 DOM 中的所有 href 链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8922899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:06:06  来源:igfitidea点击:

Get all href links in DOM

javascriptgoogle-chrome-extension

提问by user1137778

I need to write code that puts all of the href links from a webpage into an array. Here's what I have so far:

我需要编写代码,将网页中的所有 href 链接放入一个数组中。这是我到目前为止所拥有的:

var array = [];
var links = document.links;
for(var i=0; i<links.length; i++) {
  array.push(links[i].href);
}

However, this does not work on a page like Gmail's inbox, because the some of the links are within an iframe. How can I get ALLof the links, including the ones inside the iframe?

但是,这在 Gmail 收件箱之类的页面上不起作用,因为某些链接位于 iframe 内。如何获取所有链接,包括 iframe 内的链接?

Also, this is for a google chrome extension. In the manifest, I have all_frames set to true - does this make a difference?

此外,这是针对 google chrome 扩展程序的。在清单中,我将 all_frames 设置为 true - 这有什么不同吗?

Thanks

谢谢

采纳答案by Aaron Gibson

I have a method I use to access data in an IFrame. How fun that the answer is never just written down to read and use :P. Feel free to modify and abuse:

我有一种方法用于访问 IFrame 中的数据。答案永远不会只是写下来阅读和使用,这多么有趣:P。随意修改和滥用:

public HtmlElementCollection GetIFrameElements(String tmpTag, int Frame)
    {
        HtmlElementCollection tmpCollection = mWebBrowser.Document.Window.Frames[Frame].Document.Body.GetElementsByTagName(tmpTag);
        return tmpCollection;
    }

I then use it to look for whatever element Im after:

然后我用它来查找我之后的任何元素:

foreach (HtmlElement el in GetElements("input"))
        {
            if (el.GetAttribute("id").Equals("hasNoGoogleAccount"))
            {
                el.InvokeMember("click");
            }
        }

You could always change the method to loop through and get all iFrames etc blah blah but that should be enough to get you moving. Rate me! Im new

你总是可以改变方法来循环并获取所有 iFrame 等等等等,但这应该足以让你动起来。评价我!我是新来的

回答by DmitrySemenov

One thing to remember that

要记住的一件事

  1. document.links
  2. document.images
  3. document.forms
  4. document.forms[0].elements
  5. document.getElementsByName()
  6. document.getElementsByClassName()
  7. document.getElementsByTagName()
  1. 文档.links
  2. 文档.图像
  3. 文档.forms
  4. 文档.forms[0].elements
  5. document.getElementsByName()
  6. document.getElementsByClassName()
  7. document.getElementsByTagName()

are live queries to DOM objects, therefore in forLoops it may significantly slow down your execution (as i < links.length is queries on each for cycle), if you check the array length like this:

是对 DOM 对象的实时查询,因此在 forLoops 中它可能会显着减慢您的执行速度(因为 i < links.length 是对每个 for 循环的查询),如果您像这样检查数组长度:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
    array.push(links[i].href);
}

instead you better do this:

相反,你最好这样做:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0, max=links.length; i<max; i++) {
    array.push(links[i].href);
}

回答by Hogsden

Surely you're going to get 'arr is not defined' with your code to begin with?

你肯定会在你的代码中得到'arr is not defined'吗?

var array = [];
var links = document.links;
for(var i=0; i<links.length; i++) {
    arr.push(links[i].href);
}

Try:

尝试:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
    array.push(links[i].href);
}

回答by Silas S. Brown

From my Web Adjuster's bookmarklet code,

从我的Web Adjuster的书签代码中,

function all_frames_docs(c) {
    var f=function(w) {
        if(w.frames && w.frames.length) {
            var i; for(i=0; i<w.frames.length; i++) f(w.frames[i])
        } c(w.document) };
    f(window) }

You can pass any function into all_frames_docsand it will be called in turn on every frame and iframe in the current window, provided your script has access to such (i.e. it's an extension or a bookmarklet). So all you have to do now is code the function to handle each document, which can go through document.getElementsByTagName("a")or whatever, and make this function the parameter of your call to all_frames_docs.

您可以将任何函数传递给all_frames_docs它,它将在当前窗口中的每个框架和 iframe 上依次调用,前提是您的脚本可以访问此类(即它是扩展程序或书签)。所以你现在要做的就是编写处理每个文档的函数,它可以通过document.getElementsByTagName("a")或其他任何东西,并使这个函数成为你调用all_frames_docs.