What is the difference between jQuery: text() and html() ?

后端 未结 16 1477
傲寒
傲寒 2020-11-22 03:45

What the difference between text() and html() functions in jQuery ?

$(\"#div\").html(\'Linkhello\         


        
16条回答
  •  -上瘾入骨i
    2020-11-22 04:32

    Strange that no-one mentioned the Cross Site scripting prevention benefit of .text() over .html() (Although others have just mentioned that .text() escapes the data).

    It's always recommended to use .text() when you want to update data in DOM which is just data / text for the user to see.

    .html() should be mostly used to get the HTML content within a div.

提交回复
热议问题