【问题标题】:jquery template error $(...).tmpl is not a functionjquery 模板错误 $(...).tmpl 不是函数
【发布时间】:2013-05-28 03:41:20
【问题描述】:

我想使用 jquery 模板,但它不能正常工作。 这是我的标记 Html。这很简单,但我很困惑为什么它不能正常工作

 <head runat="server">
    <script src="Resource/Scripts/jquery.min.js" type="text/javascript"></script>
        <script src="Resource/Scripts/jquery-tmpl.js"></script>    
            <script id="myTmpl" type="text/x-jquery-tmpl" >
                    <tr>
                        <td><label>${urlPic}</label></td>
                        <td><label>${name}</label></td>
                        <td><label>>${count}</label</td>
                        <td><label>${price}</label></td>
                        <td><label >${sum}</label></td>
                    </tr>
              </script>
            <script type="text/javascript">
                     jQuery(document).ready(function () {
                            var data = [{ urlPic: "abc.jpg", name: "Tom", count: "3", price: "3000000
        0", sum: "40000000" } ];

    // below line raise errro                   
                         $("#myTmpl").tmpl(data).appendTo("#baskettbl2");
                    });
            </script>
  </head>

这是我的html

    <body>
    <form id="form1" runat="server">
        <table id="baskettbl2"><table>
</form>
</body>

我收到此错误:

TypeError: $(...).tmpl is not a function

编辑: 我发现它仅在第一次请求时才起作用。这个模板在我的母版页中。加载默认值时。它不起作用。但是当点击另一个页面时,它可以工作!有什么问题?我真的很困惑

【问题讨论】:

  • 你能展示一下你是如何包含你的 JS 文件的吗?
  • 是的,我编辑了我的帖子。添加js文件
  • 您是否检查过 jquery-tmpl.js 是否正确加载?并检查您的页面是否不会覆盖 $.tmpl
  • 我将上面的代码复制到原始 aspx 文件中,它可以工作!!! @KhanhTo
  • 使用网络捕获检查是否出现404错误

标签: jquery asp.net jquery-templates


【解决方案1】:

我已将这些自定义函数(属于使用 cookie)添加到我的 jquery.js 文件中,它会覆盖我的 $ 字符。我评论这些行并解决错误

    (function (factory) {
    if (typeof define === 'function' && define.amd) {
        // amd. register as anonymous module.
        define(['jquery'], factory);
    } else {
        // browser globals.
        factory(jquery);
    }
}

(function ($) {

    var pluses = /\+/g;

    function raw(s) {
        return s;
    }

    function decoded(s) {
        return decodeuricomponent(s.replace(pluses, ' '));
    }

    function converted(s) {
        if (s.indexof('"') === 0) {
            // this is a quoted cookie as according to rfc2068, unescape
            s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
        }
        try {
            return config.json ? json.parse(s) : s;
        } catch (er) { }
    }

    var config = $.cookie = function (key, value, options) {

        // write
        if (value !== undefined) {
            options = $.extend({}, config.defaults, options);

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new date();
                t.setdate(t.getdate() + days);
            }

            value = config.json ? json.stringify(value) : string(value);

            return (document.cookie = [
                config.raw ? key : encodeuricomponent(key),
                '=',
                config.raw ? value : encodeuricomponent(value),
                options.expires ? '; expires=' + options.expires.toutcstring() : '', // use expires attribute, max-age is not supported by ie
                options.path ? '; path=' + options.path : '',
                options.domain ? '; domain=' + options.domain : '',
                options.secure ? '; secure' : ''
            ].join(''));
        }

        // read
        var decode = config.raw ? raw : decoded;
        var cookies = document.cookie.split('; ');
        var result = key ? undefined : {};
        for (var i = 0, l = cookies.length; i < l; i++) {
            var parts = cookies[i].split('=');
            var name = decode(parts.shift());
            var cookie = decode(parts.join('='));

            if (key && key === name) {
                result = converted(cookie);
                break;
            }

            if (!key) {
                result[name] = converted(cookie);
            }
        }

        return result;
    };

    config.defaults = {};

    $.removecookie = function (key, options) {
        if ($.cookie(key) !== undefined) {
            // must not alter options, thus extending a fresh object...
            $.cookie(key, '', $.extend({}, options, { expires: -1 }));
            return true;
        }
        return false;
    };

}));

【讨论】:

  • 您能否详细介绍一下您的解决方案?实际上看起来并不清晰。
  • @AudriusMeškauskas 是的。为什么不
  • 检查 $ 是否被覆盖,在解决方案中搜索 noConflict 并检查覆盖 $ 的字符并使用它。以我为例,如果我使用 _$.tmpl() 而不是使用 $,那么它可以工作。
【解决方案2】:

我建议你做 2 次检查:

  • 使用浏览器内置的网络捕获来查看库是否正确加载。
  • 检查页面上的其他库或脚本是否覆盖了 $ 变量。

【讨论】:

    【解决方案3】:

    检查 $ 是否被覆盖,在解决方案中搜索 noConflict 并检查覆盖 $ 的字符并使用它。以我为例,如果我使用 _$.tmpl() 而不是使用 $,那么它可以工作。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,只需检查必须添加的“jquery.tmpl.min.js”引用,here the link file

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-23
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2012-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多