admin管理员组文章数量:1400167
is there a best way of "clearing" html inside an element using jQuery? I use .html(null)
but is .html("")
more efficient. And is there an even better/nicer way of achieving this?
is there a best way of "clearing" html inside an element using jQuery? I use .html(null)
but is .html("")
more efficient. And is there an even better/nicer way of achieving this?
- possible duplicate of How to remove an element's content with JQuery? – Shawn Chin Commented May 28, 2012 at 16:00
2 Answers
Reset to default 10Is there a reason for not just using .empty()
?
And, incidentally, if your question is about 'efficiency,' so long as efficiency is broadly parable with speed, then might I suggest JS Perf for self-testing?
Incidentally, in a JS Perf parison, with Chromium 18/Ubuntu 11.04, .empty()
seems to be consistently the faster approach.
References:
Further to the above; if you don't mind using plain ol' native JavaScript, it's even faster (on my Samsung II the DOM runs at ~82k ops/sec, on my desktop (nothing special) in Chromium 18/Ubuntu 11.04, the DOM runs at: ~860k ops/sec, as opposed to .empty()
(the next fastest) at 8.4k ops/sec).
A DOM-based approach:
var list = document.getElementsByTagName('ul')[0];
while (list.firstChild) {
list.removeChild(list.firstChild);
}
JS Perf parison of all above approaches.
References:
empty()
.
http://api.jquery./empty/
provided by jQuery
本文标签: JavaScriptjQuery html(null) vs html(3939)Stack Overflow
版权声明:本文标题:JavaScriptjQuery html(null) vs html('') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744241541a2596811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论