JavaScript / jQuery html(null)vs html('') [英] JavaScript/jQuery html(null) vs html('')
问题描述
有没有一种使用jQuery清除元素内部html的最佳方法?我使用 .html(null)
但是 .html()
更高效。是否有更好/更好的方法来实现这一目标?
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?
推荐答案
是否有理由不仅仅使用 .empty()
?
Is there a reason for not just using .empty()
?
顺便提一下,如果你的问题是关于'效率',只要效率与广泛可比速度,然后我可以建议 JS Perf 进行自我测试吗?
And, incidentally, if your question is about 'efficiency,' so long as efficiency is broadly comparable with speed, then might I suggest JS Perf for self-testing?
顺便提一下,在 JS Perf比较中,使用Chromium 18 / Ubuntu 11.04, .empty()
似乎始终是更快的方法。
参考文献:
Incidentally, in a JS Perf comparison, with Chromium 18/Ubuntu 11.04, .empty()
seems to be consistently the faster approach.
References:
除此之外;如果你不介意使用普通的本机JavaScript,它的甚至更快(在我的三星II上,DOM在我的桌面上运行〜82k ops / sec,在Chromium 18 / Ubuntu中没什么特别的) 11.04,DOM运行:~860k ops / sec,而不是 .empty()
(下一个最快),8.4k ops / sec)。
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).
基于DOM的方法:
var list = document.getElementsByTagName('ul')[0];
while (list.firstChild) {
list.removeChild(list.firstChild);
}
参考文献:
-
empty( )
。
这篇关于JavaScript / jQuery html(null)vs html('')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!