将元素附加到 jQuery 中的正文顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16327200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append element to top of body in jQuery
提问by O P
Right now Facebook wants me to throw this ugly div at the top of my page directly under <body>
现在 Facebook 想让我把这个丑陋的 div 放在我页面的顶部,直接放在下面 <body>
<div id="fb-root"></div>
I would like to avoid this by instead, appending it via JavaScript.
我想通过 JavaScript 附加它来避免这种情况。
.append('<div id="fb-root"></div>');
This places it at the bottom of the page.
这会将其置于页面底部。
</head>
<body>
<header>test</header>
<div id="fb-root"></div>
</body>
How can I 'append' it to the top?
我怎样才能将它“附加”到顶部?
</head>
<body>
<div id="fb-root"></div>
<header>test</header>
</body>
回答by undefined
Use prepend
method:
使用prepend
方法:
$('body').prepend('<div id="fb-root"></div>');
回答by Hinovska
You can use
您可以使用
$('#fb-root').detach().insertBefore('body>*:first');