How to change a text with jQuery

前端 未结 7 1631
执念已碎
执念已碎 2020-12-03 00:24

I have an h1 with id of toptitle that is dynamically created, and I am not able to change the HTML. It will have a different title depends on a pag

相关标签:
7条回答
  • 2020-12-03 00:44

    This should work fine (using .text():

    $("#toptitle").text("New word");
    
    0 讨论(0)
  • 2020-12-03 00:47

    Something like this should do the trick:

    $(document).ready(function() {
        $('#toptitle').text(function(i, oldText) {
            return oldText === 'Profil' ? 'New word' : oldText;
        });
    });
    

    This only replaces the content when it is Profil. See text in the jQuery API.

    0 讨论(0)
  • 2020-12-03 00:49

    Pretty straight forward to do:

    $(function() {
      $('#toptitle').html('New word');
    });
    

    The html function accepts html as well, but its straight forward for replacing text.

    0 讨论(0)
  • 2020-12-03 00:53

    Something like this should work

    var text = $('#toptitle').text();
    if (text == 'Profil'){
        $('#toptitle').text('New Word');
    }
    
    0 讨论(0)
  • 2020-12-03 00:56

    Cleanest

    Try this for a clean approach.

    var $toptitle = $('#toptitle');
    
    if ( $toptitle.text() == 'Profile' ) // No {} brackets necessary if it's just one line.  
      $toptitle.text('New Word');         
    
    0 讨论(0)
  • 2020-12-03 00:57

    Could do it with :contains() selector as well:

    $('#toptitle:contains("Profil")').text("New word");
    

    example: http://jsfiddle.net/niklasvh/xPRzr/

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