使用Javascript动态创建和删除Div标签

时间:2012-11-10 09:03:37

标签: javascript html

当我按下3个不同的按钮(红绿蓝)时,我想动态创建3个Div标签..当我按下红色时,我希望“红色”div的高度为我的屏幕大小,以便整个页面显示为是红色的,当我按下绿色时,我希望“红色”div的高度变为我屏幕的50%,其他50%的屏幕应该被其他div(绿色)占用..

最后当我按下蓝色按钮时,我希望所有的div都以相同的高度出现在屏幕上......

我可以创建div但是在按下所有三个按钮后按红色按钮时我无法删除以前创建的div ...

以下是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Ass 2</title>

        <script type="text/javascript">

        function fblue() {
            // document.bgColor = 'lightblue';

            selc=document.getElementById("first");
            divBlue=document.getElementById("one");
            myPara.removeChild(divBlue);



             selc=document.getElementById("first");

             divBlue=document.createElement("div");
             divBlue.id = "one";
             divBlue.style.backgroundColor = "lightblue";
             divBlue.style.height = "610px"
             divBlue.innerHTML = "dv tag created successfully";

             selc.appendChild(divBlue);
        }

        function fgreen() {

            selc=document.getElementById("first");
            divBlue=document.getElementById("one");


            divBlue.id = "one";
            divBlue.style.backgroundColor = "lightblue";
            divBlue.style.height = "305px"
            divBlue.innerHTML = "dv tag created successfully";
            selc.appendChild(divBlue);

            divGreen=document.createElement("div");
            divGreen.id = "one";
            divGreen.style.backgroundColor = "lightgreen";
            divGreen.style.height = "305px"
            divGreen.innerHTML = "dv tag created successfully";
            selc.appendChild(divGreen);

            //document.bgColor = ''; 
        }

        function fred() {

            document.getElementById("one").style.backgroundColor = 'lightblue';
            document.getElementById("one").style.width = '1104px';
            document.getElementById("one").style.height = '185px';

            document.getElementById("two").style.backgroundColor = 'red';
            document.getElementById("two").style.width = '1104px';
            document.getElementById("two").style.height = '185px';

            document.getElementById("three").style.backgroundColor = 'lightgreen';
            document.getElementById("three").style.width = '1104px';
            document.getElementById("three").style.height = '185px';
            document.bgColor = ''; 
        }

        </script>
    </head>
    <body style="margin: 0px;">
        <div style="height: 0px;" id="first"></div>

        <div id="four" style="margin-left: 415px; margin-top: 500px">
            <form>
                <input type="button" name="blu" value="Blue" onclick="javascript:fblue();">
                <input type="button" name="gre" value="Green/Red" onclick="javascript:fgreen();">
                <input type="button" name="re" value="Red/Green/Blue" onclick="javascript:fred();">
            </form>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:4)

  document.createElement("div"); 

将动态创建div

var oldChild = element.removeChild(child);
element.removeChild(child);

从DOM中删除子节点。返回已删除的节点。