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
3 Answers
Reset to default 7jQuery 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
版权声明:本文标题:javascript - Removing HTML Element by Id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744673493a2618970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论