admin管理员组文章数量:1334122
How do I check whether id
exists in the DOM or not? I used the length concept, but it gives the wrong result. I am using the jstree
plugin in my demo. I want to check whether the node exists or not. I used this
if ($('#b-a-1').length) {
alert("yes")
}
else {
alert("no")
}
However, when I run it, it gives an alert of "no", but it already exists in the DOM. Why is this?
Here is my Fiddle link: /
How do I check whether id
exists in the DOM or not? I used the length concept, but it gives the wrong result. I am using the jstree
plugin in my demo. I want to check whether the node exists or not. I used this
if ($('#b-a-1').length) {
alert("yes")
}
else {
alert("no")
}
However, when I run it, it gives an alert of "no", but it already exists in the DOM. Why is this?
Here is my Fiddle link: http://jsfiddle/fuu94/179/
Share Improve this question edited Jun 8, 2014 at 12:18 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked May 25, 2014 at 7:14 user944513user944513 3- 2 The plugin changes the markup and removes the ID, so no, it doesn't exist, and that's why you're getting the right alert, it works perfectly fine. – adeneo Commented May 25, 2014 at 7:17
- so we cann't check id is exist or not in this plugin ? is there any way to check ? – user944513 Commented May 25, 2014 at 7:21
- why do you want to use it, may you can sue alternate solution for your problem if you explain what you want to do? – LNT Commented May 25, 2014 at 7:46
3 Answers
Reset to default 6You can use $('#tree').jstree(true).get_node("b-a-1")
to check if a node with the spefified id exists. The get_node method returns false if the node does not exist.
Here is an example:
if($('#tree').jstree(true).get_node("b-a-1"))
{
alert("yes")
}else{
alert("no")
}
I also created a new fiddle based on yours to show. http://jsfiddle/mXyHL/4/
EDIT: Just want to point out that this is actually no search in the DOM because, as jfriend00 and kmoe already pointed out, the element you are looking for is collapsed and not present in the DOM. Nevertheless this method tells you if jsTree includes a node with the specified id.
When you initialise the jstree plugin using .jstree()
, it collapses the nodes and removes the child li
elements, including the one with id b-a-1
.
Then when you click to expand the b node, it creates the child li
elements again. If you click the checkId
button at this point, it will alert "yes".
Tip: use your browser's inspector (F12) to see when these elements are created.
It has to do with the jstree plugin. If you remove that code, then the items are there and your code works.
It appears that the plugin has actually removed the collapsed items from the DOM so a regular DOM query won't find them until they are expanded because they aren't in the DOM. If you look in a DOM inspector when the top level items are all collapsed, you will see that the b-a-1
item is not in the DOM, thus the jQuery selector can't find it. Expand it so you can see it and then your jQuery selector finds it.
本文标签: javascriptHow to check if an id exists in the DOM (already use length concept)Stack Overflow
版权声明:本文标题:javascript - How to check if an id exists in the DOM (already use length concept)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742359030a2459990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论