0

我正在使用 jQuery 和下划线将 HTML 片段动态添加到我的页面。该代码适用于 Firefox 和 Chrome,但不适用于 IE。

谢谢 !

<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<head>
<style>
table#uploads {
    width: 500px;
    border-spacing: 0;
    border-collapse: collapse;
}

table#uploads tr {
    width: 100%;
}

table#uploads td {
    width: 30px;
    border: 1px solid black;
}
</style>
<script type="text/template" class="template">
<% _.each(items, function(item) { %>
<tr>
    <td><%- item.reference %></td>
    <td><%- item.filename %></td>
    <td><%- item.size %></td>
    <td>
        <button>Delete</button>
    </td>
</tr>
<% }); %>
</script>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript" src="underscore.js"></script>
<script type="text/javascript">
_.templateSettings.variable = 'items';

var template = _.template(
        $('script.template').html()
);

function addUploads(result) {
    var itemCount = result['itemCount'];
    if (itemCount > 0) {
        var fragment = template(result['items']);
        var table = $('#uploads');
        table.append(fragment);
    }
}

$(function() {
    var result = {
            itemCount: 1,
            items: [
                    {
                        filename: 'image.jpg',
                        reference: 'SQ1.jpg',
                        size: '1 Kb'
                    }
            ]
    };

    addUploads(result);
});
</script>
</head>
<body>
    <table id="uploads">
    </table>
</body>
</html>
4

1 回答 1

0

我通过制作内联模板解决了这个问题:

var source = "<% _.each(items, function(item) { %><tr><td><%= item.reference %></td><td><%= item.filename %></td><td><%= item.size %></td><td><button class='delete'>Delete</button></td></tr> <% }); %> ";
var template = _.template(source);
于 2013-06-07T09:55:00.973 回答