admin管理员组文章数量:1356965
I need to check if an object has same id on DOM, because i am trying to create a modal window library,
the problem is in browsers console when i write
$("#anonexistingid");
it returns []
but in my code
if($("#"+id)!==[]){
return;
}
just does not work. What is the proper way of defining an empty array?(Because i think [] means empty array, maybe not i am not sure :D)
I need to check if an object has same id on DOM, because i am trying to create a modal window library,
the problem is in browsers console when i write
$("#anonexistingid");
it returns []
but in my code
if($("#"+id)!==[]){
return;
}
just does not work. What is the proper way of defining an empty array?(Because i think [] means empty array, maybe not i am not sure :D)
Share Improve this question edited Jul 2, 2011 at 19:44 user113716 323k64 gold badges453 silver badges441 bronze badges asked Jul 2, 2011 at 19:38 gkaykckgkaykck 2,36711 gold badges35 silver badges52 bronze badges 1-
1
In any case, two objects are only equal if they are identical (the same). Even
[] == []
returnsfalse
. – Felix Kling Commented Jul 2, 2011 at 19:45
4 Answers
Reset to default 9JQuery always returns an object. you can check it's length to see if it's empty or not:
if($("#"+id).length !== 0){
}
[] is an empty array. To test if empty, try:
if($("#"+id).length) {
//Element is found
}
try this . it returns true if object is empty , else returns false
jQuery.isEmptyObject({}) // true
jQuery.isEmptyObject({ foo: "bar" }) // false
http://api.jquery./jQuery.isEmptyObject/
You could use the size()
method.
if($("#"+id).size() > 0){
return;
}
This method returns the number of matched elements by $
.
Hope this helps. Cheers
本文标签: javascriptjquery return value of a nonexisting id or classStack Overflow
版权声明:本文标题:javascript - jquery return value of a non-existing id or class? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744064300a2584703.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论