【问题标题】:How to write JQuery template in js file如何在js文件中编写JQuery模板
【发布时间】:2012-02-15 06:02:34
【问题描述】:
我有一些写在.cshtml 文件中的JQuery 模板代码,但想将它移到js 文件中。但是js文件无法识别<script></script>。
这是tmpl 的示例。
<<
<script type="text/x-jquery-tmpl" id="searchResult">
<li><a href="#" data-name="${Name}">${Name}</a></li>
</script>
>>
谢谢。
【问题讨论】:
标签:
jquery
asp.net-mvc-3
templates
razor
jquery-templates
【解决方案1】:
大声笑,您不必在 js 文件中包含 <script> 标记。 Js 字段是“假定的”<script>,但是我不熟悉您的设置,因为您的脚本标签之间的内容似乎是非常基本的 html,应该放在 html 文件中。 Js 常用于实际的 javascript,又名:
$(function() {
$(document).find("a").css("text-decoration", "none");
}); // this is an example of somthing typically found in a js file whereas
// the following would generally be placed as in a .php VIEW file, thus MVC, Modal View Control
<ul>
<li><a href="#" data-name="${Name}">${Name}</a></li>
</ul>
还请记住,我的示例使用 jquery 标记,因此它可能无法在普通的 javascript 程序中运行
【解决方案2】:
首先我建议不要混合模板代码和脚本代码,但这是您的设计决定。
尝试删除<script> 标签,因为引擎可能会将其视为蛋糕上的蛋糕。脚本中的脚本标记。
【解决方案3】:
您是否缺少声明:
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>