获取DOM中的所有href链接 [英] Get all href links in DOM
问题描述
我需要编写代码将网页中的所有href链接放入数组中。这是我到目前为止:
var array = [];
var links = document.links;
for(var i = 0; i< links.length; i ++){
array.push(links [i] .href);
}
然而,这不适用于Gmail收件箱等网页,因为有些的链接在iframe中。我如何获得所有链接,包括iframe中的链接?
另外,这也适用于谷歌浏览器扩展。在清单中,我将all_frames设置为true - 这是否有所作为?
谢谢
我有一个方法用于访问IFrame中的数据。答案永远不会被写下来阅读和使用:
随意修改和滥用:
$ b $ pre $ public HtmlElementCollection GetIFrameElements(String tmpTag,int Frame)
{
HtmlElementCollection tmpCollection = mWebBrowser.Document.Window.Frames [Frame] .Document.Body.GetElementsByTagName(tmpTag);
返回tmpCollection;
}
然后我用它来查找任何元素Im:
foreach(HtmlElement el in GetElements(input))
{
if(el.GetAttribute(id ).Equals(hasNoGoogleAccount))
{
el.InvokeMember(click);
$ / code $ / pre
您可以随时更改方法循环并获取所有的iFrames等等等等,但这应该足以让你感动。
给我评分!即时新
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:
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 ALL of the links, including the ones inside the iframe?
Also, this is for a google chrome extension. In the manifest, I have all_frames set to true - does this make a difference?
Thanks
解决方案 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:
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
这篇关于获取DOM中的所有href链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!