本文介绍了TypeError getElementsByTagName不是函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到此问题的根源。基本上我在控制台中收到的错误消息是:

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');

我不知道为什么会这样,因为我已经包含了 jQuery 在页面本身的标题中:

I'm not sure why this is happening as I have included jQuery in the header of the page itself:

<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?

推荐答案

jQuery构造函数返回的对象没有 .getElementsByTagName()方法。

The object returned by the jQuery constructor doesn't have the .getElementsByTagName() method.

$('selector')返回一个jQuery对象。 .getElementsByTagName()是DOM元素的本机JavaScript方法。

$('selector') returns a jQuery object. .getElementsByTagName() is a native JavaScript method of DOM elements.

使用特定标记名查找元素你现在拥有的jQuery对象:

To look for elements with a certain tagname using the jQuery object you currently have:

var inputs = $('directoryresults input');
// OR
var inputs = $('directoryresults').find('input');

获取类似的节点列表 .getElementsByTagName() 将返回(注意这不完全相同,这将返回一个数组,其中 .getElementsByTagName()将返回):

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):

var inputs = $('directoryresults input').get();

注意: directoryresults ,我假设,是DOM元素的类或id。无论哪种方式,您都希望修改上面的选择器

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

这篇关于TypeError getElementsByTagName不是函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 00:58