有这样一段div布局

<div class="clearfix">
    <p class="left c">
        <span>456</span>
    </p>
    <p class="left cd">
        <span style="float: left;">789</span>
        <span>vs</span>
        <span style="float: right;">147</span>
        <em>258</em>
    </p>
    <p class="right d">
        <span>357</span>
    </p>
</div>

想根据后面传来的数据动态的创建多个这样的div,要怎么实现.

那么:<input type="button" value='add' onclick="createDiv('container')">定义一个方法;

<script type="text/javascript">
function createDiv(brother){
    brother=brother?document.getElementById(brother):document.body;
    var html='<div class="clearfix">'+
                 '<p class="left c"><span>456</span></p>'+
                 '<p class="left cd"><span style="float: left;">789</span><span>vs</span><span style="float: right;">147</span><em>258</em></p>'+
                 '<p class="right d"><span>357</span></p>'+
             '</div>';
    brother.insertAdjacentHTML('beforeend',html);
}
</script>

posted on 2016-11-10 15:00  happiness木木  阅读(450)  评论(0编辑  收藏  举报