2011-01-19 62 views
0

我正在学习有关jQuery中的模板。并且已经得到了我测试的一些示例代码。但它似乎并不奏效。jQuery模板的问题。对象不支持此方法错误

<html> 
<head> 
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js " type="text/javascript"></script> 
    <script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js" type="text/javascript" /> 
</head> 

<body> 

    <h2>ViewPage1</h2> 

    <script id="movieTemplate" type="x-jquery-tmpl"> 
     <li><b>${Name}</b> (${ReleaseYear})</li> 
    </script> 

    <div id="movieList"></div> 

    <script type="text/javascript"> 
      var movies = [ 
       { Name: "The Red Violin", ReleaseYear: "1998" }, 
       { Name: "Eyes Wide Shut", ReleaseYear: "1999" }, 
       { Name: "The Inheritance", ReleaseYear: "1976" } 
       ]; 

      // Render the template with the movies data and insert 
      // the rendered HTML under the "movieList" element 
      $("#movieTemplate").tmpl(movies).appendTo("#movieList"); 
    </script> 
</body> 

当调试我可以看到,问题是与.appendTo()方法。而且我也可以在intellisens中看到那种方法不在那里。

我做错了什么?

+0

是什么$( “#movieTemplate”)TMPL(电影)的回报? “tmpl”函数可能无法链接,因此您试图在非jQuery对象上访问jQuery方法。 调试的一个好方法是将链式调用分解为单独的语句。这使得更容易设置断点并查看每个函数的返回值。 – draeton 2011-01-19 21:22:16

回答

2

这似乎是在标题中的脚本定义一个问题:

<head> 
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js " type="text/javascript"></script> 
    <script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js" type="text/javascript" /> 
</head> 

script标签必须有结束</script>标签。基本上你的tmpl脚本没有加载。我确实注意到这是tmpl示例中的一个错误,所以不是你的错。如果您更改第二个相匹配的第一个,它工作正常:

<head> 
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js " type="text/javascript"></script> 
    <script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js" type="text/javascript"></script> 
</head> 

例: http://jsfiddle.net/UZ62w/

1

我很确定它与tmpl没有返回一个jQuery对象有关。尝试修改您的JS类似于这样的事情

var movies = [ 
      { Name: "The Red Violin", ReleaseYear: "1998" }, 
      { Name: "Eyes Wide Shut", ReleaseYear: "1999" }, 
      { Name: "The Inheritance", ReleaseYear: "1976" } 
      ], 
template = $("#movieTemplate").tmpl(movies); 

$("#movieList").append(template); 

编辑:这里有一个jsfiddle显示它的工作原理。

+1

你的作品和他的原因与这里的修改无关。它的作用是因为你改变了图书馆的加载方式。 – 2011-01-19 23:06:39

+0

谢谢你澄清和设置记录,先生。非常感谢。 :-) – Lance 2011-01-19 23:45:53

相关问题