简体   繁体   English

使用 Javascript 将元素包裹在 div 中

[英]Wrap elements inside div using Javascript

I'm feel very irritated with div alignment problems on window resize.我对窗口调整大小的 div 对齐问题感到非常恼火。 I would like to wrap elements inside div so that they won't come out of their exact positions after window resize.Is there a way possible to wrap elements inside parent <div> using Javascript.我想将元素包装在 div 中,以便它们在窗口调整大小后不会从它们的确切位置出来。有没有办法使用 Javascript 将元素包装在父<div>中。 Also suggest any best ways possible to overcome this using javascript like handling resize() & zoom() events.还建议使用处理resize()zoom()事件等 javascript 的任何最佳方法来解决这个问题。 If not suggest solutions possible by using Jquery.如果没有建议使用 Jquery 可能的解决方案。

HTML: HTML:

<div id ="top-left"></div>
<div id ="top-right"></div>
<div id ="bottom-left"></div>
<div id ="bottom-right"></div>

css: css:

div{
    width:50px;
    height:50px;
    background-color:red;
    position:absolute;
}
#top-left{
    top:0;
    left:0;
}
#top-right{
    top:0;
    right:0;
}
#bottom-left{
    bottom:0;
    left:0;
}
#bottom-right{
    bottom:0;
    right:0;
}
div#container{
    position:relative;
    border: 2px solid blue;
    width:300px;
    height:300px;
    background:green;
}

​jQuery: jQuery:

$(document).ready( function(){
    $('body').append('<div id="container"></div>');
    $('body div').not('#container').each(function() {
        var html = $(this);
        $('#container').append(html);        
    });
});​

Demo: http://jsfiddle.net/x8Wnw/演示: http : //jsfiddle.net/x8Wnw/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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