javascript - jQuery 模板未正确绑定(bind)

标签 javascript jquery

考虑以下 html:

<table>
    <thead>(something here)</thead>
    <div id="templateContainer"></div>
    <tfoot>(something here)</tfoot>
</table>

我有适当的 jquery JSON 代码将模板正确绑定(bind)到 #templateContainer 中,并且数据也成功绑定(bind)。但问题是,绑定(bind)数据后,#templateContainer 似乎没有出现在它应该在的位置,事实上它是(在开发工具中):

<div id="templateContainer"></div>
<table>
    <thead>(something here)</thead>
    <tfoot>(something here)</tfoot>
</table>

我可以知道是什么原因导致上述模板错位吗?谢谢

编辑

<script type="text/javascript">
    function filterList() {
        var filter = 25;
        PageMethods.getList(filter, OnSuccessFilter);
    }

    function OnSuccessFilter(data) {
        BindTemplate(data[0]);
    }

    function BindTemplate(data) {
        $.get('/UserControls/User/template.htm', function (template) {
            $('#templateContainer').empty();
            data = eval('(' + data + ')');
            jQuery(template).tmpl(data).appendTo('#templateContainer');
        });
    }
</script>

最佳答案

您不能输入 <div>里面 <table>那样。 <div>如您所见,将显示在表格之外。这是因为语法不正确。

您必须将包装器 ( <div id="templateContainer"></div> ) 替换为 <tr>'s<td>'s所以这将是一个可接受的 HTML 表格。

关于javascript - jQuery 模板未正确绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38939615/

相关文章:

javascript - 如何在单击时切换单选输入元素的检查状态?

Javascript 荧光灯闪烁效果

javascript - 访问嵌套 JSON 对象和数组中的值

Javascript :What does element. 属性返回?如何在 URL 中使用它?

javascript - 在 jquery mobile 中添加和删除收藏夹功能,并在单独的 ListView 中查看收藏夹项目

javascript - 在 ember-cli 中对数组模型进行排序

javascript - 如何使用 Javascript 从文本框中获取文本的值

javascript - JavaScript 函数中返回未定义

javascript - 无法使 "Enter"键调用函数而不是提交页面

javascript - 带字符边框的数字输入