简体   繁体   English

html脚本标签不使用类型javascript <script type =“text / html”>?

[英]html script tag not using type javascript <script type=“text/html”>?

I was checking out the source on an html page and came across this 我正在检查html页面上的源代码并遇到了这个问题

<script id="searchItemTemplate" type="text/html"> 
    <# var rows = Math.floor((Model.RecordsPerPage - 1) / 3 + 1);
       for (var i = 0; i < rows; ++i){
        var startIdx = i * 3;
        var endIdx = startIdx + 3;
    #>
//etc .... 
</script>

I have never seen this before. 我以前从未见过这个。 What is script type="text/html" . 什么是脚本type="text/html" I don't know if it makes a difference but this was on a .aspx page. 我不知道它是否有所作为,但这是在.aspx页面上。

Is this some sort of place holder to be parsed and eval() later? 这是某种占位符需要解析和eval()以后吗?
Does anyone know what this is? 有谁知道这是什么?
Can someone who has used this method explain the benefits? 使用这种方法的人可以解释这些好处吗?

Script elements that have an unknown content-type are simply ignored, in this case, the browser doesn't know how to execute a text/html script. 简单地忽略具有未知内容类型的脚本元素,在这种情况下,浏览器不知道如何执行text/html脚本。

It's a common technique used by some JavaScript templating engines. 这是一些JavaScript模板引擎使用的常用技术。

See also: 也可以看看:

It's a trick that I first saw in a John Resig blog post. 这是我在John Resig博客文章中首次看到的一个技巧。 It's used for stuff like holding a template to be expanded later. 它用于举行模板以便稍后扩展的东西。 The browser won't make any attempt to execute it. 浏览器不会尝试执行它。

The "benefits"? 好处”? Well, it's a lot neater than keeping a string in your Javascript code directly. 嗯,它比直接在你的Javascript代码中保留一个字符串要简洁得多。 Because you don't have multi-line strings in Javascript, it's messy to define a template like that. 因为你在Javascript中没有多行字符串,所以定义这样的模板很麻烦。

edit — ha ha yes, CMS has linked the very blog post in his answer! 编辑 - 哈哈是的,CMS在他的回答中链接了博客文章!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 System.Web.Optimization 时如何将 type=&quot;text/javascript&quot; 添加到脚本标记 - How do I add type=“text/javascript” to a script tag when using System.Web.Optimization 加载模块脚本失败 - text/html 而不是 application/javascript - Failed to load module script – text/html instead of application/javascript 在/ BODY标记的末尾渲染自定义HTML /脚本 - Render custom HTML/Script at end of /BODY tag 在javascript中找到html控件的类型 - find the type of html controls in javascript input type =“ password”在IE 11上使用HTML 5显示文本 - input type=“password” shows text using HTML 5 on IE 11 在JavaScript脚本中将img HTML标记与对C#函数的调用相结合添加到ASP.NET页面 - Adding an img HTML tag combined with a call to C# function to an ASP.NET page inside JavaScript script 在ASP.NET MVC 4中使用Scripts.Render时,在“script”标记中生成“type”属性 - Produce a “type” attribute in the “script” tag when using Scripts.Render in ASP.NET MVC 4 如何将内容类型设置为html和纯文本 - How to set content type to html and plain text 从asp.net mvc控制器操作获取查询字符串的值,以<script type=“text/javascript”> - Get value of a query string from asp.net mvc controller action to <script type=“text/javascript”> 存储html css javascript的最佳数据类型 - Best data type to store html css javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM