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

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

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

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


        
16条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 04:26

    Actually both do look somewhat similar but are quite different it depends on your usage or intention what you want to achieve ,

    Where to use:

    • use .html() to operate on containers having html elements.
    • use .text() to modify text of elements usually having separate open and closing tags

    Where not to use:

    • .text() method cannot be used on form inputs or scripts.

      • .val() for input or textarea elements.
      • .html() for value of a script element.
    • Picking up html content from .text() will convert the html tags into html entities.

    Difference:

    • .text() can be used in both XML and HTML documents.
    • .html() is only for html documents.

    Check this example on jsfiddle to see the differences in action

    Example

提交回复
热议问题