0

单击按钮时,我正在尝试将新的“应用程序”附加到我的“应用程序列表”。

JS

$(".appCreate" ).click(newApp);

function newApp() {
    var facebookTemp = $("#facebook-template").html();

    var appName = $(this).data("appName");
    var appSize = $(this).data("appSize");
    var appTemp = $(this).data("appTemp");

    $("<div class=\"app" + appName + appSize + "\"></div>").html(appTemp).appendTo(".AppList");
};

HTML

<body>

    <section id="AppBox">
        <div class="AppList">

<!-- == Facebook == -->
            <div id="facebook-template">
                <div class="App facebook Size170x290">
                    <h1>Hello Test</h1>
                </div>
            </div>
        </div>
    </section>

<!-- == Settings == -->
    <section id="dialog-settings" class="dialog" title="Settings">
        <button data-appName="facebook" data-appSize="Size170x290" data-appTemp="facebookTemp" class="appCreate">
        Facebook</button>
    </section>
</body>

CSS

#facebook-template {
    display: none;
}
.facebook {
    background:linear-gradient(to bottom, #133783 0px, #102E6D 100%) repeat scroll 0 0 #133783;
}
.facebook { top:120px; left:0; }
#AppBox {
    position:fixed;
    top:0;
    bottom:0;
    left:0;
    right:0;
    margin:0;
}
.AppList {
    position:absolute;
    height:100%;
    width:100%;
    margin:0;
    padding:0;
}
.App {
    position:absolute;
    display:block;
    margin:5px;
    padding:0;
    box-shadow:0 0 5px 1px black;
    color:#FFFFFF;
    cursor:move;
}
.Size170x290 {height:170px;width:290px;}

基本上事情没有出现,我不知道是什么原因造成的。

4

1 回答 1

5

You lost the context of your click handler.

This was an obvious first mistake:

$(".appCreate" ).click(newApp);

Second mistake is data names. Change to:

var appName = $(this).data("appname");

Notice the case. Convert all names to lower case.

I also added handlebars.js because of the conversation in the comments.

New working code:

$(".appCreate" ).click(newApp);

function newApp() {
    var facebookTemp = $("#facebook-template").html();

    var appName = $(this).data("appname");
    var appSize = $(this).data("appsize");
    var appTemp = $(this).data("apptemp");

    var template = Handlebars.compile(facebookTemp);
    var html = template({
        app : appName,
        facebook : appTemp,
        size : appSize
    });

    $(".AppList").append(html);
};

Live DEMO && CODE.

于 2013-04-21T12:08:03.333 回答