简体   繁体   中英

wrapAll not wrapping plain text

There is some HTML that looks like this:

  <body>
    Do
    all
    declarations for
    the same
    <TT>static</TT> function
    or variable
    have to include the storage class <TT>static</TT>?
  </body>

When I do $('body').children().wrapAll("<div class='container'><div class='row'><div class='col-md-12'></div></div></div>"); to wrap everything into a .container , it works except it doesn't encompass the plain text:

  <body>
   <div class='container'>
    <div class='row'>
     <div class='col-md-12'>
      <TT>static</TT>
      <TT>static</TT>
     </div>
    </div>
   </div>
    Do
    all
    declarations for
    the same
    function
    or variable
    have to include the storage class ?
  </body>

How do I fix this so it preserves the original structure?

children does not return text nodes. You will need to use contents .

$('body').contents().wrapAll("<div class='container'><div class='row'><div class='col-md-12'></div></div></div>");

 $(function(){ $('#children').children().wrapAll("<div class='container'><div class='row'><div class='col-md-12'></div></div></div>"); $('#contents').contents().wrapAll("<div class='container'><div class='row'><div class='col-md-12'></div></div></div>"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h2>With Children:</h2> <div id="children"> Do all declarations for the same <TT>static</TT> function or variable have to include the storage class <TT>static</TT>? </div> <h2>With Contents:</h2> <div id="contents"> Do all declarations for the same <TT>static</TT> function or variable have to include the storage class <TT>static</TT>? </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM