Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从jquery模板问题开始_Javascript_Jquery_Jquery Templates - Fatal编程技术网

Javascript 从jquery模板问题开始

Javascript 从jquery模板问题开始,javascript,jquery,jquery-templates,Javascript,Jquery,Jquery Templates,我有:jquery1.6.min、jquery-tmpl.js最新测试版和knockout-1.2.0.js。我使用的是一个非常简单的示例,但我无法获得jquery模板来渲染,我无法找出原因,简单地说,我可以通过使用firebug扫描dom来查看dom中的元素-我出现了一些tmp=annonymousjquery,$item,但数据不会在dom中渲染 模板: <script id="bookTemplate" type="text/x-jquery-tmpl"> <h2

我有:jquery1.6.min、jquery-tmpl.js最新测试版和knockout-1.2.0.js。我使用的是一个非常简单的示例,但我无法获得jquery模板来渲染,我无法找出原因,简单地说,我可以通过使用firebug扫描dom来查看dom中的元素-我出现了一些tmp=annonymousjquery,$item,但数据不会在dom中渲染

模板:

<script id="bookTemplate" type="text/x-jquery-tmpl">
    <h2>${title}</h2>
    price: ${formatPrice(price)}
</script>
jscode:

<script type="text/javascript">
        $(document).ready(function() {
        // Create an array of books
        var books = [
            { title: "ASP.NET 4 Unleashed", price: 37.79 },
            { title: "ASP.NET MVC Unleashed", price: 44.99 },
            { title: "ASP.NET Kick Start", price: 4.00 },
            { title: "ASP.NET MVC Unleashed iPhone", price: 44.99}];

        function formatPrice(price) {
            return "$" + price.toFixed(2);
        }

        // Render the books using the template
        $('#bookTemplate').tmpl(books).appendTo('#bookContainer');
        });
    </script>

在模板中调用formatPrice,但在闭包中定义函数。要做到这一点,价格必须在全球范围内。一个简单的方法是将函数分配给windiw.formatPrice

我觉得自己像个十足的白痴。。。你给我看的东西帮了我的忙,但我没有添加我要添加的实际容器:@Haroon Oh。我假设你的HTML:D中有这个,如果它解决了你的问题,接受它:出于某种原因,我不得不等4分钟,行吗。谢谢您:
window.formatPrice = function formatPrice(price) {
    return "$" + price.toFixed(2);
}