How to append div inside other div

后端 未结 5 1580
北荒
北荒 2021-01-26 03:37

I want to append div inside mytext div but it is updating text outside of the div




        
相关标签:
5条回答
  • 2021-01-26 03:48

    Try like this

    $(function(){
       var jj= $('body').append('<div class="mytext"></div>');
       $('.mytext').html('<div>min</div>')  ;     
     });
    
    0 讨论(0)
  • 2021-01-26 03:51

    jj refers to 'body' element.

    $(function () {
        var $mydiv = $('<div>min</div>'),
            $body = $('body');
        $body.append('<div class="mytext">');
        var $mytext = $body.find('.mytext:last');
        $mytext.append($mydiv);
    })
    
    0 讨论(0)
  • 2021-01-26 04:03

    you don't need jj

    <head>
    <script type="text/javascript" src="jquery-1.8.3.js"></script>
    <script type="text/javascript">
    $(function(){
      $('body').append('<div class="mytext"><div>min</div></div>')
    })
    </script>
    </head>
    <body>
    </body>
    
    0 讨论(0)
  • 2021-01-26 04:08
    var myDiv = $('<div class="mytext"></div>');
    myDiv.append('<div>min</div>');
    $('body').append(myDiv);
    

    The append function doesn't append html to the document. It appends elements to the document object tree.

    You could, however, just write this on one line like this, if you wish.

    $('body').append('<div class="mytext"><div>min</div></div>');
    

    Boom.

    0 讨论(0)
  • 2021-01-26 04:10
    $(function(){
        var jj= $('<div class="mytext"></div>');
        body.append(jj.append('<div>min</div>'));
    });
    
    0 讨论(0)
提交回复
热议问题