admin管理员组文章数量:1293013
I have a string and I need to check if there is in the page a div with id = to my string.
How can i do this with jquery?
Like:
if (isset $(mystring))
// do somethinhg
I have a string and I need to check if there is in the page a div with id = to my string.
How can i do this with jquery?
Like:
if (isset $(mystring))
// do somethinhg
Share
Improve this question
asked May 13, 2011 at 23:06
anonanon
4 Answers
Reset to default 5if ($("#" + mystring).length > 0)
alert(mystring + " exists!");
else
alert("element with id " + mystring + " does not exist..");
Have this code either in the bottom of the page or wrapped inside $(document).ready()
function otherwise even if the element exists further in the page it won't be recognized.
I'd go with:
if ( $('#' + mystring)[0] ) {
jQuery is not needed to do this. In native js:
if (document.getElementById("mystring")) {
alert("exists");
}
You can do this with the is()
function. http://api.jquery./is/
Here is a quick example: http://jsfiddle/hRLgw/
本文标签: javascriptcheck if there is a div with a certain idStack Overflow
版权声明:本文标题:javascript - check if there is a div with a certain id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741565882a2385719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论