最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if an id exists in the DOM (already use length concept)? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 6

You 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.

发布评论

评论列表(0)

  1. 暂无评论