javascript 为什么 getElementsByTagName 返回未定义?

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

Why is getElementsByTagName returning undefined?

javascripthtmlfirefoxdom

提问by Mason Wheeler

I'm trying to call document.getElementsByTagName, and I'm getting back undefinedas a result, no matter what parameter I pass. (Even if I pass "*".)

我正在尝试调用document.getElementsByTagNameundefined结果我回来了,无论我传递什么参数。(即使我通过了“*”。)

I tried Googling for it, but all the search results were about elements of the getElementsByTagName result array being undefined. What I'm getting is undefinedas the result itself, and it's driving me up the wall.

我尝试谷歌搜索,但所有搜索结果都是关于未定义的 getElementsByTagName 结果数组的元素。我得到的是undefined结果本身,它把我推上墙。

Does anyone know what can cause this? (Using Firefox 12.0. In Chrome I get the expected results.)

有谁知道什么会导致这种情况?(使用 Firefox 12.0。在 Chrome 中我得到了预期的结果。)

EDIT: OK, here's sample code:

编辑:好的,这是示例代码:

function buttonClick(){
   var xhr = new XMLHttpRequest();
   var msg = document.getElementById('message');
   var buttons = document.getElementsByTagName("button");
   var button, i;
   for (i = 0; i < buttons.length; ++i){
      button = buttons[i];
      msg.removeChild(button);
   }

   xhr.onreadystatechange = function() {
        if(xhr.readyState == 4){
            handleResult(xhr.responseText, msg);
        }
   };
   xhr.open("POST", location.href, true);
   xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
   xhr.send("cmd=MyCommand");
}

And the getElementsByTagNamealways returns undefined, whether I trace it in Firebug's Script tab or call it from the Console tab. (Also in Firebug, since this seems to be confusing people. Apparently there are way too many consoles floating around.).

并且getElementsByTagName总是返回undefined,无论我是在 Firebug 的 Script 选项卡中跟踪它还是从 Console 选项卡调用它。(同样在 Firebug 中,因为这似乎让人们感到困惑。显然有太多的控制台在浮动。)。

As proof, here's what I've been getting when I tried to use the Firebug console:

作为证据,以下是我尝试使用 Firebug 控制台时得到的结果:

>>> document.getElementsByTagName("button");
undefined
>>> msg.getElementsByTagName("button");
undefined
>>> msg.getElementsByTagName
getElementsByTagName()
>>> msg.getElementsByTagName("BUTTON");
undefined
>>> msg.getElementsByTagName("*");
undefined
>>> document.getElementsByTagName("*");
undefined
>>> document.getElementsByTagName("body");
undefined

The markup is (or ought to be) irrelevant. It's a valid, well-formed HTML page with some buttons and other elements on it. This JS function is attached to the onclickof one of the buttons. But it looks something like this:

标记是(或应该是)不相关的。它是一个有效的、格式良好的 HTML 页面,上面有一些按钮和其他元素。此 JS 函数附加到onclick其中一个按钮的 。但它看起来像这样:

<html xmlns="http://www.w3.org/1999/xhtml"><head>
blah
</head>
<body>
<script type="text/javascript" src="/myJS.js"></script>
<div id="page-container">
   <div id="message"><button onclick="buttonClick();">Button 1</button><button onclick="ButtonClick2()">Button 2</button></div>

</div>

</body></html>

回答by Esailija

edit:

编辑:

This is a bug in firebug and is fixed by upgrading to 1.10.0a7

这是 firebug 中的一个错误,通过升级到1.10.0a7 已修复



Because it is impossible for this method to return undefined, there are 2 possibilities:

因为这个方法是不可能返回的undefined,所以有2种可能:

  • Your debugging tools are lying to you
  • document.getElementsByTagNameis not referencing the original host object. It should print function getElementsByTagName() {[native code]}when referenced in console.
  • 你的调试工具在骗你
  • document.getElementsByTagName未引用原始宿主对象。它应该function getElementsByTagName() {[native code]}在控制台中引用时打印 。

You should be able to reliably to see if it's in fact undefined(in firefox) with this:

您应该能够通过以下方式可靠地查看它是否实际上undefined(在 Firefox 中):

delete window.alert;
window.alert(buttons);

The deleteis a NOOP if window.alertis already referencing the original host object, otherwise it will restore it.

delete是,如果一个NOOPwindow.alert已经引用原始主机的对象,否则就会恢复。

If it alertsundefined, you should be able to do

如果它发出警报undefined,你应该能够做到

delete document.getElementsByTagName

to restore the host object reference.

恢复宿主对象引用。

All console references here refer to the built in Web Console that comes with firefox by default.

此处的所有控制台引用均指默认情况下 firefox 附带的内置 Web 控制台。

回答by Ayman Safadi

Isn't REPL a stand-alone, browser-independent JavaScript environment? While, in your case, in just happens to be running in your browser as a plugin, it's supposed to mimic a "clean room" per say...

REPL 不是一个独立的、独立于浏览器的 JavaScript 环境吗?虽然在您的情况下,它恰好作为插件在您的浏览器中运行,但它应该模仿一个“洁净室”...

To summarize this guy's answer: document.getElementById() returns null when using mozrepl (but not in firebug)

总结一下这个人的答案:document.getElementById() 在使用 mozrepl 时返回 null(但不在 firebug 中)

By default, you'r in the browser'scontext, not the document's.

默认情况下,您处于浏览器的上下文中,而不是文档的上下文中。

Try this to switch to the document instead:

试试这个来切换到文档:

repl.enter(content)

回答by Tom McDonald

I got the problem when I made a difficult to see syntax error. I used parenthesiswhen I should have used square brackets

当我很难看到语法错误时,我遇到了问题。我用括号时,我应该用方括号

Wrong:

错误的:

selectedItem._element.childNodes(0).getElementsByTagName('input').item();

Right:

对:

selectedItem._element.childNodes[0].getElementsByTagName('input').item();

See the difference? Note, the top syntax works is older versions of IE, like IE8, but it doesn't work in ie10, ie11, Edge etc

看到不同?请注意,顶级语法适用于较旧版本的 IE,例如 IE8,但不适用于 ie10、ie11、Edge 等

回答by user10941360

You can't alert an Array, you should do a for loop to alert it. Example:

你不能提醒Array,你应该做一个 for 循环来提醒它。例子:

var x = document.getElementsByTagName('a');
for (var i = 0, c = x.length ; i < c ; i++) {
    alert('Element n° ' + (i + 1) + ' : ' + x[i]);
}