How do I change the text of a span element using JavaScript?

后端 未结 13 672
悲&欢浪女
悲&欢浪女 2020-11-22 09:21

If I have a span, say:

 hereismytext 

How do I use JavaScript to change "hereism

13条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 09:39

    In addition to the pure javascript answers above, You can use jQuery text method as following:

    $('#myspan').text('newtext');
    

    If you need to extend the answer to get/change html content of a span or div elements, you can do this:

    $('#mydiv').html('new text');
    

    References:

    .text(): http://api.jquery.com/text/

    .html(): http://api.jquery.com/html/

提交回复
热议问题