You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery 3.3.1项目中出现tmpl() is not a function错误求助

Fixing "tmpl() is not a function" in jQuery 3.3.1

Hey there, let's get that annoying tmpl() is not a function error sorted out for your project!

The Root of the Problem

You mentioned you thought jQuery 1.5+ includes templating in the core—and you were partially right! Up through jQuery 1.7, the templating module was part of the core library. But starting with jQuery 1.8, the official team removed templating from the core and moved it to a separate, community-maintained plugin. That's why your 3.3.1 version doesn't recognize the tmpl() method by default.

Solution 1: Add the jQuery Templating Plugin

To keep using the tmpl() syntax you already have, you just need to include the standalone templating plugin after your jQuery script.

Update your HTML to include the plugin:

<script src="js/jquery-3.3.1.min.js"></script>
<!-- Add this line to include the templating plugin -->
<script src="js/jquery.tmpl.min.js"></script>
<script id="itemTemplate" type="text/x-jQuery-tmpl">
  <tr>
    <td>${title}</td>
    <td><span class="fa fa-plus-square"></span>${amount}<span class="fa fa-minus-square"></span></td>
  </tr>
</script>

Make sure you grab a version of the plugin compatible with jQuery 3.x—community-maintained builds are available for this purpose.

Solution 2: Switch to a Native Templating Approach

If you'd rather avoid adding another plugin, you can use vanilla JavaScript template literals with jQuery's DOM manipulation methods. Here's a quick example matching your template structure:

// Sample data to render
const items = [
  { title: "Weekly Groceries", amount: 65 },
  { title: "Fuel", amount: 40 }
];

// Target your table body (adjust the selector to match your actual HTML)
const $tableBody = $("table tbody");

items.forEach(item => {
  // Build the row HTML with template literals
  const rowHtml = `
    <tr>
      <td>${item.title}</td>
      <td><span class="fa fa-plus-square"></span>${item.amount}<span class="fa fa-minus-square"></span></td>
    </tr>
  `;
  // Append the row to the table
  $tableBody.append(rowHtml);
});

This approach is lightweight and requires no extra dependencies. For more complex templating needs, you could also explore popular engines like Handlebars or Mustache.

内容的提问来源于stack exchange,提问作者Bart Friederichs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:32:04