set text in jquery

前端 未结 4 991
借酒劲吻你
借酒劲吻你 2021-02-03 20:20

If I want to set the text of a

to \"Test message here\", do I do:

 $(\'
\').text(\'Test
相关标签:
4条回答
  • 2021-02-03 20:59

    It's preferable to use

    $("#error").html('Test message here') 
    

    instead of simply

    .text('Text Msg');
    
    0 讨论(0)
  • 2021-02-03 21:02
     $('#error').text('Test message here');
    
    0 讨论(0)
  • 2021-02-03 21:04

    You create a new div and set its text, but you don't insert it anywhere. What you need to do is:

    var el = $('<div id="error">').text('Test message here');
    $(document).append(el);
    

    or, if the div is already there:

    $("#error").text('Test message here');
    
    0 讨论(0)
  • 2021-02-03 21:04

    Try:

    $('<div id="error"></div>').text('Test message here');
    

    You also need to insert the new element somewhere in the page.

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