注册 登录
编程论坛 JavaScript论坛

怎么设置新生成div的宽度,点击第二次添加后自动换到下行显示

lzy740744969 发布于 2016-12-29 10:21, 2465 次点击
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>录入表格</title>
<style>

#myDiv{
    width:100px;
    height:50px;
    background-color:#ccc;
    }

</style>
<script>

</script>

</head>

<body>
<input type="text" id="text01" name="what"  style="width:100px"/>
<input type="text" id="text02" name="where"/>
<input type="button" id='B' name="plus" value="添加" onclick="plus()"/>
<hr>
<div id=myDiv ><p></p></div>



<script type="text/javaScript">
function plus() {
        var newdiv1 = document.createElement("div");     //创建div
        newdiv1.style.width = "50";        // 设置宽度
         newdiv1.style.height ="10";
        var textnode = text01.value+"\n";
        newdiv1.innerHTML = textnode;    //给新div添加内容text01
        //newdiv1.style.width=100+"px";
        document.getElementById("myDiv").appendChild(newdiv1);    //把div插入到最后
   
        
        var newdiv2 = document.createElement("div");
        newdiv2.style.width = "50";        // 设置宽度
         newdiv2.style.height ="10";
        var textnode = text02.value+"\n";
        newdiv2.innerHTML = textnode;
        newdiv1.appendChild(newdiv2);
        
}
</script>


</body>
</html>
1 回复
#2
lzy7407449692016-12-29 11:11
1