What is the difference between .empty().append() and .html()?

前端 未结 6 1197
心在旅途
心在旅途 2020-12-18 08:27

Using jQuery, what\'s the performance difference between using:

$(\'#somDiv\').empty().append(\'text To Insert\')

and

$(\'         


        
6条回答
  •  时光说笑
    2020-12-18 09:04

    difference between append() and html() in jQuery

    .append() and .html() are the most useful methods in jQuery. But these are far different from one another, .append() add some value to the existing one. Where .html() do the same but it removes the old value first.

    Here is an example:

    • test

    Now I will use .append() to add one

  • , For that I will write:

    
    

    The output of this jQuery will be:

    • test
    • test1

    Now if I use .html() to add one

  • , For that I will write:

    
    

    The output of this Script will be:

    • test1

    Here in this example .append() add one extra

  • , whether .html() removes the old one with new one. This is the main difference between .append() and .html() in jQuery.

提交回复
热议问题