简体   繁体   中英

How to wrap all the body contents in a div with jquery?

In a JQuery plugin I'm writing I want to hide all the contents of the <body> tag by wrapping them in a div and calling .hide() . What is the best way to wrap existing DOM elements into a single parent element?

For example:

I want to turn this

<body>
    <h1>Hello World!</h1>
    <p>This is a sample document to be manipulated by a JQuery plugin!</p>
</body>

into this

<body>
    <div id="originalBodyContents">
        <h1>Hello World!</h1>
        <p>This is a sample document to be manipulated by a JQuery plugin!</p>
    </div>
</body>

Thanks!

This should do it...

$(document).ready(function() {
    $('body').wrapInner('<div class="new" />');
    $('.new').hide();
});

Here's a little fiddle to show something similar in action.

您不想做以下任何具体原因?

$('body').hide();​​​​

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