JavaScriptjQuery html(null) vs html('') - Stack Overflow

is there a best way of "clearing" html inside an element using jQuery? I use .html(null) but

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?

Share Improve this question asked May 28, 2012 at 15:53 RuslanRuslan 10.2k15 gold badges57 silver badges92 bronze badges 1
  • possible duplicate of How to remove an element's content with JQuery? – Shawn Chin Commented May 28, 2012 at 16:00
Add a ment  | 

2 Answers 2

Reset to default 10

Is 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744241542a4564727.html

相关推荐

  • JavaScriptjQuery html(null) vs html('') - Stack Overflow

    is there a best way of "clearing" html inside an element using jQuery? I use .html(null) but

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信