0

如何添加newObject到我的数组shapeArr?我需要添加一个for循环吗?

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <style type="text/css">
            ul{ list-style-type:none;}
            div{ width:300px; height:200px; background-color:#0066cc; }
        </style>    
    </head>
    <body>
        <ul id="placeholder"></ul>
        <a href="#" id="btn">Add</a>

        <script type="text/javascript">

            function add(){

                function draw(){

                    var template = document.createElement("li");
                    template.innerHTML = "<div></div><a href='#' class='update'>Update</a>";                

                    document.getElementById("placeholder").appendChild(template);
                }

                var newObject = new draw();

                var shapeArr = [];

            }

            var btn = document.getElementById("btn");
            btn.addEventListener("click", add, false);

        </script>       
    </body>
</html>
4

2 回答 2

1

使用Array.push().

shapeArr.push(newObject);
于 2012-10-16T23:50:01.693 回答
0

您可以使用push()JavaScript Array 的方法将新元素添加到末尾:

var newObject = new draw();
var shapeArr  = [];

// add the new Object
shapeArr.push(newObject);
于 2012-10-16T23:48:10.017 回答