如何检查一个dom元素是否存在
2010-11-05 15:37
801 查看
http://www.aaronrussell.co.uk/blog/check-if-an-element-exists-using-jquery/
Monday, 15 September 2008
The obvious thing would simply be to wrap a selector in an
With a jQuery selector we can also use the
Tagged: JavaScript, jQuery, Web Development
Related articles:
jQuery Plugin released: Simply Smart Login Fields
jQuery plugin released: Simply Countable
What do you think of the IE9 platform preview?
Share this article
SubscribeDiggdel.icio.usStumbleUpon![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/601acda36e6842fab2ca66ec9fcd3de0)
Tiago Soczek 2:36 pm, 10 October 2008// this works too..
if ($(“#mydiv”).length){
// do something here
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/1b8d46806bc03c44e888b7009c2b6332)
Aaron 2:43 pm, 10 October 2008That’s true Tiago – cheers for sharing. I’ll leave my explanation as it is because I think the “greater than zero” operator illustrates why it would return true or not. But you solution is slightly more elegant
![](http://www.aaronrussell.co.uk/wp-includes/images/smilies/icon_smile.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/ebac08b79e1bd53b45e15e3a94499624)
nickyflavier 8:28 am, 16 October 2008You could also use
if($(“#mydiv”).size()) {
//do something
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/7647dbbb86612c1b3a74daee2d19dd54)
mipme kampfkoloss 8:40 am, 25 March 2009Thx mate, just the thing i was looking for. Keep it up!
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/b78f07c242e04a9772dc10030a56e901)
gonzalo 5:04 pm, 7 August 2009Works like a charm
![](http://www.aaronrussell.co.uk/wp-includes/images/smilies/icon_biggrin.gif)
thanks
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/caa06ff2a19dc933ecccbd2f8e66988d)
Patrick Fitzgerald 3:20 pm, 28 August 2009The easiest (smallest) test I have found is:
if ($(‘#mydiv’)[0]) {
// do something
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/feadfe3ab449d3b34ed4a102f38edbb3)
Phil 3:23 pm, 9 November 2009Nice and simple – just what I need.
Cheers Aaron
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/adcd7ac8f30c75fa66be50b9af1715bb)
Erik 8:19 pm, 15 December 2009Nice, thanks! exactly what I needed
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/dee378ec81344d65a60a7885667f8f4b)
Jason 4:02 pm, 13 January 2010Excellent tip!! thank you kindly.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/930cdba4a3308ac6ae9c11d1cafef5fb)
Tim 8:48 pm, 16 January 2010Correct me if I’m wrong (which I very well could be), but I don’t think you’re using the jQuery selector engine without doing $(“#mydiv”) or jQuery(“#mydiv”). You’re using some default javascript selector.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/1b8d46806bc03c44e888b7009c2b6332)
Aaron 10:56 pm, 16 January 2010Tim – Something funky is happening with my code snippets on this site. Some characters are being rendered in white text on a white background (the $ symbol in this case). Haven’t got a clue why, but I’ll look into it and try and fix it soon.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/8ddb39efaa31c34e7ed3f95ceca8bbe1)
john 11:36 pm, 27 January 2010looking at your css the pre tag that surrounds the $ is set to #FFFFFF by the rule #main .article pre unlike the rest of the contnet which is contained within span tag that set the colour to something else
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/1b8d46806bc03c44e888b7009c2b6332)
Aaron 10:39 am, 28 January 2010Cheers John! Saved me looking into it
![](http://www.aaronrussell.co.uk/wp-includes/images/smilies/icon_smile.gif)
Code on my site is now fixed.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/2cccc1dc16fb517e1cdcdce6f180bece)
Radu 3:20 pm, 12 February 2010I don’t know but any element in my page is length = 0 . Even html.length = 0.
Just tried with jQuery
$(‘#someDiv’).parents(‘html’).length;
return 0.
Firebug show all my elements 0 length …
Any ideas where the problem could be ?
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/b9bf038335c84d611355cfa43f2d2728)
alex 9:08 am, 26 February 2010thanks mate! was going crazy over this. thought id kill IE7 one more time because he kept throwing script errors at me
![](http://www.aaronrussell.co.uk/wp-includes/images/smilies/icon_biggrin.gif)
well thanks and have a good one!
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/036fdf17317db5d61cd7fc3f84b75b49)
Phil 6:42 pm, 16 June 2010That’s just the thing I was looking for, Aaron. Thanks a lot.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/c019fe3699e77dbe11f1b17998bb7a42)
Antonio 10:12 am, 22 June 2010Hi,
I have got a question about this method. Is it possible to execute this “if exist” method on a page based on the DIV that exist in another page?
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/7d28380461f91539a2cbdb608c363ea7)
Johnny 11:06 am, 19 August 2010Why use jQuery for this when just plain ass javascript does the job?
if(document.getElementById(“id”)) {
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/10/1b8d46806bc03c44e888b7009c2b6332)
Aaron 1:12 pm, 24 August 2010@Johnny – because with jQuery you could use any old selector, not just an ID.
Monday, 15 September 2008
How to check whether an element exists using jQuery
When using jQuery (or any other JavaScript library for that matter), have you ever wondered how to test whether an element exists using a selector? Well, maybe you haven’t, but I have – lots of times – so I thought I’d share how it’s done here because it’s not as simple as it seems.The obvious thing would simply be to wrap a selector in an
ifstatement, right?
if ($("#mydiv")){ // do something here }Well, wrong – that won’t work! When you use a selector, jQuery will always return an object. Therefore the
ifstatement will always be true and never be false. In the case of an element that does not exist on the page, jQuery will return an object with nothing in it – an empty object. And therein lies our solution.
With a jQuery selector we can also use the
lengthproperty, which will return the size of the object. Do you see where I’m heading with this? That’s right, lets just change our
ifstatement to:
if ($("#mydiv").length > 0){ // do something here }Now our code works because when jQuery returns an empty object, the
lengthproperty will return zero and therefore the
ifstatement will be false. Hurrah!
Tagged: JavaScript, jQuery, Web Development
Related articles:
jQuery Plugin released: Simply Smart Login Fields
jQuery plugin released: Simply Countable
What do you think of the IE9 platform preview?
Share this article
SubscribeDiggdel.icio.usStumbleUpon
19 fantastic comments
Tiago Soczek 2:36 pm, 10 October 2008// this works too..
if ($(“#mydiv”).length){
// do something here
}
Aaron 2:43 pm, 10 October 2008That’s true Tiago – cheers for sharing. I’ll leave my explanation as it is because I think the “greater than zero” operator illustrates why it would return true or not. But you solution is slightly more elegant
![](http://www.aaronrussell.co.uk/wp-includes/images/smilies/icon_smile.gif)
nickyflavier 8:28 am, 16 October 2008You could also use
if($(“#mydiv”).size()) {
//do something
}
mipme kampfkoloss 8:40 am, 25 March 2009Thx mate, just the thing i was looking for. Keep it up!
gonzalo 5:04 pm, 7 August 2009Works like a charm
![](http://www.aaronrussell.co.uk/wp-includes/images/smilies/icon_biggrin.gif)
thanks
Patrick Fitzgerald 3:20 pm, 28 August 2009The easiest (smallest) test I have found is:
if ($(‘#mydiv’)[0]) {
// do something
}
Phil 3:23 pm, 9 November 2009Nice and simple – just what I need.
Cheers Aaron
Erik 8:19 pm, 15 December 2009Nice, thanks! exactly what I needed
Jason 4:02 pm, 13 January 2010Excellent tip!! thank you kindly.
Tim 8:48 pm, 16 January 2010Correct me if I’m wrong (which I very well could be), but I don’t think you’re using the jQuery selector engine without doing $(“#mydiv”) or jQuery(“#mydiv”). You’re using some default javascript selector.
Aaron 10:56 pm, 16 January 2010Tim – Something funky is happening with my code snippets on this site. Some characters are being rendered in white text on a white background (the $ symbol in this case). Haven’t got a clue why, but I’ll look into it and try and fix it soon.
john 11:36 pm, 27 January 2010looking at your css the pre tag that surrounds the $ is set to #FFFFFF by the rule #main .article pre unlike the rest of the contnet which is contained within span tag that set the colour to something else
Aaron 10:39 am, 28 January 2010Cheers John! Saved me looking into it
![](http://www.aaronrussell.co.uk/wp-includes/images/smilies/icon_smile.gif)
Code on my site is now fixed.
Radu 3:20 pm, 12 February 2010I don’t know but any element in my page is length = 0 . Even html.length = 0.
Just tried with jQuery
$(‘#someDiv’).parents(‘html’).length;
return 0.
Firebug show all my elements 0 length …
Any ideas where the problem could be ?
alex 9:08 am, 26 February 2010thanks mate! was going crazy over this. thought id kill IE7 one more time because he kept throwing script errors at me
![](http://www.aaronrussell.co.uk/wp-includes/images/smilies/icon_biggrin.gif)
well thanks and have a good one!
Phil 6:42 pm, 16 June 2010That’s just the thing I was looking for, Aaron. Thanks a lot.
Antonio 10:12 am, 22 June 2010Hi,
I have got a question about this method. Is it possible to execute this “if exist” method on a page based on the DIV that exist in another page?
Johnny 11:06 am, 19 August 2010Why use jQuery for this when just plain ass javascript does the job?
if(document.getElementById(“id”)) {
}
Aaron 1:12 pm, 24 August 2010@Johnny – because with jQuery you could use any old selector, not just an ID.
相关文章推荐
- 在VC里如何检查一个文件是否存在
- 如何在前端用js判断一个元素在一个数组中是否存在
- SharePoint 如何检查一个SPWeb是否存在?
- 如何使用jQuery判断一个元素是否存在
- 如何使用JQuery去判断一个元素(对象)是否存在
- C语言中如何检查一个文件是否存在以及权限等信息
- javascript中如何判断一个元素在页面中是否存在(转)
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
- 使用jquery怎么去检查一个元素是否存在
- [SQL] 如何在SQL Server2005数据库中检查一个表是否存在,如存在就删除表记录,如不存在就建表.
- 前端框架对页面的损害+如何判断一个dom是否存在某个属性
- 如何利用C库检查一个路径下是否存在某文件
- PHP检查一个数组内是否存在指定元素in_array
- SharePoint 如何检查一个SPWeb是否存在
- javascript(jquery)中如何判断一个元素在页面中是否存在
- 如何检查你的 Linux 系统是否存在 Meltdown 或者 Spectre 漏洞 | Linux 中国
- java HashSet 如何判断元素是否存在
- 如何判断一个已知的表中是否存在某个字段,如果不存在就创建它.
- js 判断一个元素是否存在
- 如何判断一个链表是否有环? 2、如果链表为存在环,如果找到环的入口点?