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

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

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

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


        
相关标签:
16条回答
  • 2020-11-22 04:41

    Well in simple term.

    html()-- to get inner html(html tags and text).

    text()-- to get only text if present inside(only text)

    0 讨论(0)
  • 2020-11-22 04:41

    text function set or retrieve the value as plain text, otherwise, HTML function set or retrieve the value as HTML tags to change or modify that. If you want to just change the content then use text(). But if you need to change the markup then you have to use hmtl().

    It's a dummy answer for me after six years, Don't mind.

    0 讨论(0)
  • 2020-11-22 04:42

    I think that the difference is to insert html tag in text() you html tag do not functions

    $('#output').html('You are registered'+'<br>'  +'  '
                         + 'Mister'+'  ' + name+'   ' + sourname ); }
    

    output :

    You are registered <br> Mister name sourname
    

    replacing text() with html()

    output

    You are registered
    Mister name sourname 
    

    then the tag <br> works in html()

    0 讨论(0)
  • 2020-11-22 04:43

    The first example will actually embed HTML within the div whereas the second example will escape the text by means of replacing element-related characters with their corresponding character entities so that it displays literally (i.e. the HTML will be displayed not rendered).

    0 讨论(0)
提交回复
热议问题