Using jQuery, how do I change the elements html value? (div)

前端 未结 8 1643
孤街浪徒
孤街浪徒 2020-12-16 16:43

Using a ajax request I want to change content of my div.

202

So I want to change the content to a differen

相关标签:
8条回答
  • 2020-12-16 17:07

    This would changed the inner text of your HTML element.

    $('#d1').text(parseInt(requestResponse)++);
    
    0 讨论(0)
  • 2020-12-16 17:12

    Use the text function:

    $("#d1").text($("#d1").text() + 1);
    
    0 讨论(0)
  • 2020-12-16 17:13
    $("#di").html('My New Text');
    

    Check out the jQuery documentation.

    If you wanted to increment the number, you would do

    var theint = parseInt($("#di").html(),10)
    theint++;
    $("#di").html(theint);
    

    P.S. Not sure if it was a typo or not, but you need to include the # in your selector to let jQuery know you are looking for an element with an ID of di. Maybe if you come from prototype you do not expect this, just letting you know.

    0 讨论(0)
  • 2020-12-16 17:19

    Unless you're embedding html like <b>blah</b> I'd suggest using $("#di").text() as it'll automatically escape things like <, > and &, whereas .html() will not.

    0 讨论(0)
  • 2020-12-16 17:20

    if your value is a pure text (like 'test') you could use the text() method as well. like this:

    $('#d1').text('test'); Or $('#d1').html('test');
    

    anyway, about the problem you are sharing, I think you might be calling the JavaScript code before the HTML code for the DIV is being sent to the browser. make sure you are calling the jQuery line in a <script> tag after the <div>, or in a statement like this:

    $(document).ready(
        function() {
            $('#d1').text('test');
        }
    );
    

    this way the script executes after the HTML of the div is parsed by the browser.

    0 讨论(0)
  • 2020-12-16 17:22
    $("#div1").innerHTML="your text goes here..";
    
    0 讨论(0)
提交回复
热议问题