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

后端 未结 13 645
悲&欢浪女
悲&欢浪女 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

    I use Jquery and none of the above helped, I don't know why but this worked:

     $("#span_id").text("new_value");
    
    0 讨论(0)
  • 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('<strong>new text</strong>');
    

    References:

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

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

    0 讨论(0)
  • 2020-11-22 09:40

    For this span

    <span id="name">sdfsdf</span>
    

    You can go like this :-

    $("name").firstChild.nodeValue = "Hello" + "World";
    
    0 讨论(0)
  • 2020-11-22 09:42

    EDIT: This was written in 2014. You probably don't care about IE8 anymore and can forget about using innerText. Just use textContent and be done with it, hooray.

    If you are the one supplying the text and no part of the text is supplied by the user (or some other source that you don't control), then setting innerHTML might be acceptable:

    // * Fine for hardcoded text strings like this one or strings you otherwise 
    //   control.
    // * Not OK for user-supplied input or strings you don't control unless
    //   you know what you are doing and have sanitized the string first.
    document.getElementById('myspan').innerHTML = 'newtext';
    

    However, as others note, if you are not the source for any part of the text string, using innerHTML can subject you to content injection attacks like XSS if you're not careful to properly sanitize the text first.

    If you are using input from the user, here is one way to do it securely while also maintaining cross-browser compatibility:

    var span = document.getElementById('myspan');
    span.innerText = span.textContent = 'newtext';
    

    Firefox doesn't support innerText and IE8 doesn't support textContent so you need to use both if you want to maintain cross-browser compatibility.

    And if you want to avoid reflows (caused by innerText) where possible:

    var span = document.getElementById('myspan');
    if ('textContent' in span) {
        span.textContent = 'newtext';
    } else {
        span.innerText = 'newtext';
    }
    
    0 讨论(0)
  • 2020-11-22 09:42

    (function ($) {
        $(document).ready(function(){
        $("#myspan").text("This is span");
      });
    }(jQuery));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <span id="myspan"> hereismytext </span>

    user text() to change span text.

    0 讨论(0)
  • 2020-11-22 09:47

    For modern browsers you should use:

    document.getElementById("myspan").textContent="newtext";
    

    While older browsers may not know textContent, it is not recommended to use innerHTML as it introduces an XSS vulnerability when the new text is user input (see other answers below for a more detailed discussion):

    //POSSIBLY INSECURE IF NEWTEXT BECOMES A VARIABLE!!
    document.getElementById("myspan").innerHTML="newtext";
    
    0 讨论(0)
提交回复
热议问题