js将一个DIV 插入到另外一个标签里。

如何将.aa用JS插入到.bb里,说明{bb不能加“id”因为id已经被别的程序占了。只能用class名了。}

<style>
.aa{width:100px;height:100px;background-color:#eeeeee;position:relative; z-index: 1;  }
.bb{width:200px;height:200px;background-color: #FF0000;padding:30px;}
</style>
<div class="aa"></div>
<embed class="bb" src="helloworld.swf" />
<script type="text/javascript">
$(function(){
    var a = $(".aa"), b = $(".bb");
    $("button").click(function(){
        b.append(a);
    });

});
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14

1111111111

done

$(function(){ var a = $(".a"), b = $(".b"); $("button").click(function(){ b.append(a); }); });.aa { width: 100px; height: 100px; background-color: #eeeeee; position: absolute; z-index: 9999; margin: 75px; } .bb { width: 200px; height: 200px; background-color: #FF0000; padding: 30px; }
    <div class="aa"></div>
    <embed class="bb" src="helloworld.swf" />

$("button").click(function(){
$("p:first").addClass("bb");
});

试一试jquery的addClass方法

图片说明

$(function(){ var a = '<div class="aa"></div>'; var b = $(".bb"); //b必须准确定位,或者确认是否指定位置是正确的 alert("检验b的正确性:" + b.html()); $("button").click(function(){ b.append(a); }); });$(function() { $(".bb").append($(".aa")) })

$(".bb").load(" .aa");

其实你想要的效果是将aa显示在bb上面吧:
给他们一个父容器,父容器样式position:relative;
然后给aa样式position:absolute;即可

完整代码:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <style>
            .aa{width:100px;height:100px;background-color:#eeeeee;position:relative; z-index: 1;  }
            .bb{width:200px;height:200px;background-color: #FF0000;padding:30px;}

            .aa{
            background-color:#0F0;
            }

            .container{
                position:relative;
            }

            .positionToAbsolute{
                position:absolute;
            }
        </style>
    </head>
    <body>
        <div>
            <div class="container">
                <div class="aa"></div>
                <embed class="bb" src="http://www.w3school.com.cn/i/helloworld.swf" />
            </div>
        </div>
        <button type="button" id="btn">测试</button>
    </body>

    <script>
        $(document).ready(function(){
            $('#btn').on('click', function(){
                if($('.aa').hasClass("positionToAbsolute")){
                    $('.aa').removeClass("positionToAbsolute");
                }else{
                    $('.aa').addClass("positionToAbsolute");
                }
            });
        });
    </script>
</html>