最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "<HTMLImageElement> has no method" error for an object that appears to exist - Stac

programmeradmin3浏览0评论

I'm using Chrome and the build in developer tools. What I do the following in the console

$('.votes > a > img')[0]

I get this

<img src="/myimage_png">

But if I do

$('.votes > a > img')[0].get(0).tagName

I get

TypeError: Object #<HTMLImageElement> has no method 'get'

I don't understand why sometimes I'm able to do .get(0).tagName and sometimes I'm not (depending on what my selector is of course)

I'm using Chrome and the build in developer tools. What I do the following in the console

$('.votes > a > img')[0]

I get this

<img src="/myimage_png">

But if I do

$('.votes > a > img')[0].get(0).tagName

I get

TypeError: Object #<HTMLImageElement> has no method 'get'

I don't understand why sometimes I'm able to do .get(0).tagName and sometimes I'm not (depending on what my selector is of course)

Share Improve this question asked Feb 16, 2012 at 2:57 BrandBrand 1,7011 gold badge25 silver badges33 bronze badges 1
  • Use either [0] or .get(0), not both. – Kevin B Commented Feb 16, 2012 at 3:04
Add a ment  | 

2 Answers 2

Reset to default 5

.get() retrieves a specified DOM element from a list of elements. the $('.votes ...') stuff call returns such an element list. You dereference this list with [0], fetching the first of the found nodes. That means you're no longer working with a DOMNode list, you're working with a DOMElement, and a DOMElement has no .get() method.

The numberical indexer of a jQuery object (using [0]) will return the DOM element that is at that position in the jQuery results array.

In your example of

$('.votes > a > img')[0].get(0).tagName

What you're doing is using the indexer to get back the DOM element then attempting to use the get method against it.

Using the indexer or get will result in the same thing as they both return the DOM element at a particular point in the jQuery results array (the only main difference is get() with no arguments returns only the DOM elements).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论