单击按钮时添加元素

时间:2015-04-19 19:03:03

标签: javascript html css html5 css3

JavaScript的:

    <script type="text/javascript">

    function createRow() {
        var d = document.createElement("section");
        d.className = "gallery-row";
        document.getElementById("gallery").appendChild(d);

    }

    </script>

HTML:

    <section id="container-main">
        <section id="gallery">
            <section class="gallery-row">
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
            </section>
            <section class="gallery-row">
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
            </section>
            <section class="gallery-row">
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
                <img src="../images/circles/myresume-a.png" alt="image" width="150px" height="150px" />
            </section>
        </section>
        <section id="load-more"><button onclick="createRow();">Load More</button></section>
    </section>

问题:

简单地说,脚本不会在具有ID“gallery”的部分中的最后一个元素之后创建一个section元素。几个小时我一直在努力,我不知道该怎么办。如果有人能够指出我做错了什么,我将不胜感激。我查看了文档,但无济于事。

1 个答案:

答案 0 :(得分:1)

你做得很好,没有任何东西,所以你不能看到它。 试试这个,你就会看到它:

function createRow() {
    var d = document.createElement("section");
    d.className = "gallery-row";
    d.innerHTML = "test"
    document.getElementById("gallery").appendChild(d);

}