javascript document.getElementsByTagName("a") 缺少链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7977198/
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-10-26 01:54:50  来源:igfitidea点击:

document.getElementsByTagName("a") misses a link

javascriptgreasemonkey

提问by BloodyRain2k

I was working on a script for Greasemonkey (FX7) trying to remove certain links and found out that for some reason one that was present in the source, not hidden or constructed by JS, didn't show up in the array that that function returns.

我正在为 Greasemonkey (FX7) 编写一个脚本,试图删除某些链接,并发现由于某种原因,源代码中存在的一个不是由 JS 隐藏或构建的,没有出现在该函数返回的数组中.

If that one would have been constructed via JS upon running that page it wouldn't wonder me but it's sitting right behind another link that gets found.

如果那个页面是在运行该页面时通过 JS 构建的,它不会让我感到奇怪,但它就在另一个被找到的链接后面。

So has anyone an idea why this is happening and how I could work around it?

那么有没有人知道为什么会发生这种情况以及我如何解决它?

var links = document.getElementsByTagName("a");
for (var l in links){
  if (links[l].href == "blah"){ ... }
}

Thats how I was trying to work with them, a bit cut down as I had some more checks to not run into nulls and such.

这就是我试图与他们合作的方式,因为我有更多的检查以防止遇到空值等,所以有点减少。

On a sidenote: I was wondering why that function also returns null entries at all.

旁注:我想知道为什么该函数也返回空条目。

Edit: I passed this problem long since I asked for help and found a nice way to do it:

编辑:自从我寻求帮助并找到了一个很好的方法来解决这个问题很久以来,我就解决了这个问题:

for (var i = 0, l; l = links[i]; i++) { }

This keeps setting l to the current link until there aren't any left. Works nice.

这会一直将 l 设置为当前链接,直到没有任何链接为止。很好用。

回答by Yuriy Rozhovetskiy

The for...in statement loops through the properties of an object. In this particular case you are iterating over Array object properties. Try to use this script instead:

for...in 语句循环遍历对象的属性。在这种特殊情况下,您正在迭代 Array 对象属性。尝试改用此脚本:

var links = document.getElementsByTagName("a");
for (var l = 0; l < links.length; l++){
  if (links[l].href == "blah"){ ... }
}

回答by VickyC

var links = document.getElementsByTagName('a');
  for (var i=0; i<links.length; i++){
     if (links[i].href.match('blah')){...}
  };

回答by Carlos Machado

getElementsByTagName ("area") [0] returns the value of its href attribute and not HTMLAnchorElement

getElementsByTagName ("area") [0] 返回其 href 属性的值而不是 HTMLAnchorElement

回答by MattiSG

for … instatements loop through the propertiesof an object, not only its values, just like @Yuriy said. You'll need to learn some Javascript to understand this (sorry, couldn't find any direct pointer to this part after a few minutes of Googling).

for … in语句循环遍历对象的属性,而不仅仅是它的值,就像@Yuriy 所说的那样。你需要学习一些 Javascript 才能理解这一点(抱歉,在谷歌搜索几分钟后找不到任何直接指向这部分的指针)。

Basically, you need to understand that objects in JS also include “methods”. When you're using a for … inloop, you find an object's values but also its “methods” and other properties.

基本上,你需要明白JS中的对象也包括“方法”。当您使用for … in循环时,您会找到一个对象的值以及它的“方法”和其他属性。

So, either use @Yuriy's indexed loop… or better, use the hasOwnProperty()method (MDN doc), that allows to avoid the very important caveat @Kolink mentioned.

所以,要么使用@Yuriy 的索引循环……或者更好,使用hasOwnProperty()方法(MDN doc),它可以避免@Kolink 提到的非常重要的警告。

Your loop should look like:

您的循环应如下所示:

var links = document.getElementsByTagName('a');
for (var l in links) {
    if (! links.hasOwnProperty(l))
        continue; // this goes straight to the next property

    if (links[l].href == "blah") { ... }
}