按钮单击时添加动态元素

时间:2014-02-13 09:48:24

标签: javascript html button

当我点击按钮添加时,我想要一个动态按钮添加到div中。我试过但不能这样做。在代码中,我无法弄清楚错误在哪里。这是我的代码。

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<html>
    <head>
        <title>Content</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- Bootstrap -->
        <link href="stylesheets/bootstrap.min.css" rel="stylesheet" type="text/css">
        <link href="stylesheets/bootstrap.css" rel="stylesheet" type="text/css">
        <link href="stylesheets/bootstrap-theme.min.css" rel="stylesheet" type="text/css">
        <link href="stylesheets/bootstrap-theme.css" rel="stylesheet" type="text/css">
        <script type="text/javascript">
            function addinfo()
            {
                //Create an input type dynamically.   
                element = document.createElement("button");
                element.className='btn btn-default';
                var t=document.createTextNode("Edit");
                element.appendChild(t);
                element.id=t;
                //var account=document.getElementById('newaccname').value;

                var foo = document.getElementById("acc");
//                //Append the element in page (in span).  
                foo.appendChild(element);
//                    var d = document.getElementById('acc');
//                    d.appendChild(i);
            }
        </script>
    </head>
    <body>
        <button type="submit" name="addaccount" class="btn btn-default" onlick="addinfo()">Add</button>
<div id="acc" style="width:500px;height:100px;border:1px solid #000;"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的代码中有拼写错误。 onlick - &gt;的onclick

onlick="addinfo()"

显然应该是

onclick="addInfo()"