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

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

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

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


        
16条回答
  •  轮回少年
    2020-11-22 04:31

    Use .text(…) when you intend to display the value as a simple text.

    Use .html(…) when you intend to display the value as a html formatted text (or HTML content).

    You should definitely use .text(...) when you’re not sure that your text (e.g. coming from an input control) do not contain characters/tags that has special meaning in HTML. This is really important because this could result in the text will not display properly but it could also cause that an unwanted JS script snippet (e.g. coming from another user input) to be activated.

提交回复
热议问题