Jquery: why $("#id_pop")[0] is illegal?
In my code when I write code like:
$("#id_pop")[0]
t开发者_开发知识库here is an error. When I correct it to:
var $d = $("#id_pop");
$d[0]....
it's ok. Why?
[0]
should work, i.e. get you a DOM object. But you can't continue using jQuery methods once you get back a normal DOM object.
you can also get a DOM object from a jQuery selector like this:
$('#id_pop').get(0);
Also are you sure you only have one element with the id id_pop
. If there is then $('#id_pop')[0]
should work.
See working example here: http://jsbin.com/udace3
You are using an ID selector, you should not be getting an array but the jquery object itself. In the jQuery documentation of using the #id,
Each id should only be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on...
Isn't it better to use .eq() instead of .get()?
$('#id_pop').eq(0);
Or for that matter:
$('#id_pop:eq(0)');
精彩评论