TypeError getElementsByTagName is not a function issue

匿名 (未验证) 提交于 2019-12-03 00:50:01

问题:

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

回答1:

Does anyone have any ideas what might be causing this?

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


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

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

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

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



回答2:

getElementsByTagName is a method you will find on Element and Document objects.

$('directoryresults') will return a jQuery object (containing any <directoryresult> elements … so nothing if you are working on an HTML document).

to use getElementsByTagName, you need to extract the elements from the jQuery object:

$('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 for loop.

That said, you should generally use the find method instead:

$('directoryresults').find('input') 

… or just use a descendant combinator in the first place:

$('directoryresults input') 

As noted earlier, directoryresults won'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.



回答3:

You are ussing a DOM API mixed with jQuery API sintax:

it's document.getElementsByTagName('input');



回答4:

The first error is not specific if directoryresults is a class or an ID

Nor do you tell if a target item or the item you wish to call


If you use jQuery by TagName type this:

var inputs = $('input'); 

if you want put values in a div

$.each(inputs, function(){     $('div').append( $(this).val() ); }); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!