【问题标题】:Using jquery-tmpl on responseText在 responseText 上使用 jquery-tmpl
【发布时间】:2017-10-14 10:47:50
【问题描述】:

我目前正在尝试填充通过在服务器上的另一个文件夹上使用 ajax get 获得的 jquery 模板。我想填写通过 .tmpl({..}) 获得的 responseTexts。可悲的是,这没有奏效。这就是我所做的。

var a = $.ajax({
   method: 'GET',
   url : 'TemplateUrl/template.html'
});
$.when(a).done(function(){
   $(a.responseText).tmpl({...});
});

responseText 是来自 SharePoint 网站的一段非常简单的 html,如下所示

    "<div>
<td class="ms-formbody">
    <!--    FieldName="{{html FieldName}}"
            FieldInternalName = "{{html FieldName}}"
            FieldType = "SPFieldText" -->
    {{html Text}}
</td>
</div>"

当我试图填写模板时,我得到了这个

Uncaught TypeError: Failed to construct 'Text': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

也许你们有一个想法。会很好。在此先感谢

问候克里斯

【问题讨论】:

    标签: javascript jquery ajax sharepoint-2013 jquery-templates


    【解决方案1】:

    好的,一旦我得到答案,答案就不是那么难而且很合乎逻辑。模板引擎需要脚本包装器才能正常工作。所以html需要看起来像这样

    <script type="text/x-jQuery-tmpl">
        <div>
            <td class="ms-formbody">
                <!--    FieldName="{{html FieldName}}"
                        FieldInternalName = "{{html FieldName}}"
                        FieldType = "SPFieldText" -->
                {{html Text}}
            </td>
            </div>
    </script>
    

    【讨论】:

      【解决方案2】:

      它引发错误的原因是因为您试图将 temp() 作为函数调用而不是实例化它。

      尝试在 $(a.responseText).tmpl({...}) 前面使用 new 关键字。

      【讨论】:

      • 感谢您的快速回复。还尝试了新的 $(a.responseText).tmpl({...});可悲的是它给了我同样的结果。也先做 var b = new $(a.responseText); b.tmpl({...});不工作
      猜你喜欢
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 2013-02-27
      相关资源
      最近更新 更多