admin管理员组

文章数量:1391928

I am using the following code to remove an element from the DOM tree:

 function onItemDeleted(name) {

           $("#" + name).remove();                       

       }

Would this be bad for performance since I am not indicating any parent for the element. The element is a TR contained in a TABLE element. The DOM search for this element will start at the top which might be BODY. So would it be something like this:

BODY => DIV => TABLE => TR (found)

If I find the parent of TR which is TABLE would the search be like this:

TABLE -> TR

I don't know if above will be true since I think search will always start at the root node.

I am using the following code to remove an element from the DOM tree:

 function onItemDeleted(name) {

           $("#" + name).remove();                       

       }

Would this be bad for performance since I am not indicating any parent for the element. The element is a TR contained in a TABLE element. The DOM search for this element will start at the top which might be BODY. So would it be something like this:

BODY => DIV => TABLE => TR (found)

If I find the parent of TR which is TABLE would the search be like this:

TABLE -> TR

I don't know if above will be true since I think search will always start at the root node.

Share Improve this question edited Feb 14, 2010 at 18:00 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Feb 14, 2010 at 17:59 azamsharpazamsharp 20.1k38 gold badges147 silver badges230 bronze badges 2
  • Do you experience any performance issues? – Felix Kling Commented Feb 14, 2010 at 18:02
  • No I don't experience any performance issues. – azamsharp Commented Feb 14, 2010 at 18:04
Add a ment  | 

3 Answers 3

Reset to default 7

jQuery optimises for ID searches. So, $("#" + name) is effectively the same as $(document.getElementById(name)). From the source, line 120 (1.4):

// HANDLE: $("#id")
} else {
    elem = document.getElementById( match[2] );

The difference in performance would likely be negligible.

I guess that when you find elements by ID, the lookup time should be O(1) since there can be only one element with that ID.

本文标签: javascriptRemoving HTML Element by IdStack Overflow