Javascript TypeError getElementsByTagName 不是函数问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26848368/
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
TypeError getElementsByTagName is not a function issue
提问by Javacadabra
I'm having trouble getting to the source of this problem. Basically the error message I am getting in my console is:
我很难找到这个问题的根源。基本上,我在控制台中收到的错误消息是:
TypeError: $(...).getElementsByTagName is not a function
When I click through to the line it is occuring on it is here:
当我点击到它正在发生的行时,它在这里:
var inputs = $('directoryresults').getElementsByTagName('input');
I'm not sure why this is happening as I have included jQueryin the header of the page itself:
我不确定为什么会发生这种情况,因为我已将其包含jQuery在页面本身的标题中:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>
Does anyone have any ideas what might be causing this?
有没有人有任何想法可能导致这种情况?
采纳答案by George
Does anyone have any ideas what might be causing this?
有没有人有任何想法可能导致这种情况?
The object returned by the jQuery constructor doesn't have the .getElementsByTagName()method.
jQuery 构造函数返回的对象没有该.getElementsByTagName()方法。
$('selector')returns a jQuery object. .getElementsByTagName()is a native JavaScript method of DOM elements.
$('selector')返回一个 jQuery 对象。.getElementsByTagName()是 DOM 元素的原生 JavaScript 方法。
To look for elements with a certain tagname using the jQuery object you currently have:
要使用您当前拥有的 jQuery 对象查找具有特定标记名的元素:
var inputs = $('directoryresults input');
// OR
var inputs = $('directoryresults').find('input');
To get a like-for-like node list that .getElementsByTagName()would return (note this isn't exactly the same, this will return an array where .getElementsByTagName()will return a HTMLCollection):
要获得.getElementsByTagName()将返回的同类节点列表(注意这并不完全相同,这将返回一个数组,其中.getElementsByTagName()将返回一个HTMLCollection):
var inputs = $('directoryresults input').get();
Note: directoryresults, I am assuming, is either a class or id of a DOM element. Either way you'll want to amend the selector above
注意:directoryresults我假设是 DOM 元素的类或 id。无论哪种方式,您都需要修改上面的选择器
回答by Quentin
getElementsByTagNameis a method you will find on Element and Document objects.
getElementsByTagName是一种可以在 Element 和 Document 对象上找到的方法。
$('directoryresults')will return a jQuery object (containing any <directoryresult>elements … so nothing if you are working on an HTML document).
$('directoryresults')将返回一个 jQuery 对象(包含任何<directoryresult>元素……如果您正在处理 HTML 文档,则什么都没有)。
to use getElementsByTagName, you need to extract the elements from the jQuery object:
要使用getElementsByTagName,您需要从 jQuery 对象中提取元素:
$('directoryresults')[0].getElementsByTagName
The above will only get the first element from the jQuery object (and it assumes that there will be at least one element) so you should probably replace the hard coded [0]with a forloop.
上面只会从 jQuery 对象中获取第一个元素(并且它假设至少会有一个元素)所以你应该[0]用for循环替换硬编码。
That said, you should generally use the findmethod instead:
也就是说,您通常应该使用该find方法:
$('directoryresults').find('input')
… or just use a descendant combinator in the first place:
...或者首先使用后代组合器:
$('directoryresults input')
As noted earlier, directoryresultswon't find anything in a valid HTML document. You probably want to prefix it with .or #depending on what you are actually trying to match.
如前所述,directoryresults不会在有效的 HTML 文档中找到任何内容。您可能希望使用.或#取决于您实际尝试匹配的内容作为前缀。
回答by Wilfredo P
You are ussing a DOM API mixed with jQuery API sintax:
您正在使用与 jQuery API 语法混合的 DOM API:
it's document.getElementsByTagName('input');
它是 document.getElementsByTagName('input');
回答by Andres Separ
The first error is not specific if directoryresults is a class or an ID
如果 directoryresults 是一个类或一个 ID,第一个错误不是特定的
Nor do you tell if a target item or the item you wish to call
你也不知道是目标物品还是你想调用的物品
If you use jQuery by TagName type this:
如果您按 TagName 使用 jQuery,请键入:
var inputs = $('input');
if you want put values in a div
如果你想把值放在一个 div 中
$.each(inputs, function(){
$('div').append( $(this).val() );
});

