javascript jquery 模板中的 <script> 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4788525/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<script> tag inside jquery template
提问by Carlos Mu?oz
Backgroud:
背景:
I have this template that includes videos from youtube being loaded with swfobject.
我有这个模板,其中包含来自 youtube 的视频,这些视频正在加载 swfobject。
Question:
问题:
Is there a way to include a script tag...
有没有办法包含脚本标签...
<script type="text/javascript">
</script>
inside a jQuery Template??
在 jQuery 模板中??
<script id="filaVideoTemplate" type="text/x-jQuery-tmpl">
    <!-- Some HTML here -->
    <script type="text/javascript">
        <!-- Some javascript here -->
    </script>
</script>
Obviously it doesn't work directly, is there any way to achive the same thing without the script inside other script?
显然它不能直接工作,有没有办法在没有其他脚本中的脚本的情况下实现同样的事情?
回答by Ronny Wang
<script id="filaVideoTemplate" type="text/x-jQuery-tmpl">
    <!-- Some HTML here -->
    <script type="text/javascript">
        <!-- Some javascript here -->
    {{html "</sc"+"ript>"}}
</script>
You can use {{html "</sc"+"ript>"}} replace </script>
您可以使用 {{html "</sc"+"ript>"}} 替换 </script>
回答by David Tang
Not sure if this is the only way, but you can insert the script as raw HTML for the template:
不确定这是否是唯一的方法,但您可以将脚本作为模板的原始 HTML 插入:
<script id="filaVideoTemplate" type="text/x-jQuery-tmpl">
    <!-- Some HTML here -->
    {{html innerScript}}
</script>
Then:
然后:
var innerScript = '<script type="text/javascript"><!-- Some javascript here --></script>';
$('#filaVideoTemplate').tmpl({innerScript: innerScript});
回答by Mass Dot Net
Ronny Wang's answer offered a good place to start for my problem. However, his solution didn't work for me -- the .. tags in the body of the jQuery template was causing problems on my MVC3 Razor view page.
Ronny Wang 的回答为我的问题提供了一个很好的起点。但是,他的解决方案对我不起作用——jQuery 模板正文中的 .. 标签在我的 MVC3 Razor 视图页面上引起了问题。
Fortunately, I stumbled across a solution ( details @ https://github.com/jquery/jquery-tmpl/issues/94) that works and requires only a minor change. Here's Ronny's answer, but with a fix that should make it MVC3-compatible:
幸运的是,我偶然发现了一个解决方案(详细信息@ https://github.com/jquery/jquery-tmpl/issues/94),该解决方案只需稍作改动即可工作。这是 Ronny 的答案,但有一个修复应该使它与 MVC3 兼容:
<script id="filaVideoTemplate" type="text/x-jQuery-tmpl">
    <!-- Some HTML here -->
    <scr{{= ""}}ipt type="text/javascript">
        <!-- Some javascript here -->
    </scr{{= ""}}ipt>
</script>
Looks like both the opening and closing tag just need to be broken up. Hope this helps!
看起来开始和结束标签都需要分解。希望这可以帮助!
回答by Thulasiram
<script id="filaVideoTemplate" type="text/x-jQuery-tmpl">
   <!-- Some HTML here -->  
</script>
var data = [{ 'Id': '1', 'Name': 'a' }, { 'Id': '2', 'Name': 'b' }, { 'Id': '3', 'Name': 'c'}];
var renderedHtml = $('<div />').html($('#filaVideoTemplate').tmpl(data)).append(('<script type="text/javascript"><!-- Some javascript here --></endScript>').replace(/endScript/g, 'script')).html();
if data is single not a collection above code is correct. else ( data is collection ) the script tag used to bind multiple times ( data.length times ).
如果数据是单一的,则上面的代码不正确。else ( data is collection ) 用于绑定多次的脚本标签 ( data.length times )。
回答by Amir Rezvani
not sure what you asking but: you can use ajax request and put your template inside of (js) file . my template_script.js the use ajax like so:
不确定您要问什么,但是:您可以使用 ajax 请求并将您的模板放在 (js) 文件中。我的 template_script.js 像这样使用 ajax:
$('the element').load('template_script.js').then( OK , fail );
function OK(){
  do something if request was ok.
}
function fail(){
  do something if request was not ok.
}
