HTML 标签如何在 script 标签内工作?
例如,查看 Joel Spolsky 的公开职业简介 的源代码
<script type="text/html" id="stackexchangeanswerswidget">
<h3>Top Answers</h3>
<div class="answers">
</div>
</script>
<script type="text/html" id="topanswer">
<div class="top-answer">
<div class="top-answer-stats">{{= shared.htmlEncode(Score) }}</div>
<span class="top-answer-title"><a href="{{=AnswerLink}}">{{= shared.htmlEncode(Title) }}</a></span>
<a class="add-answer">add</a>
<br class="clear" />
</div>
</script>
<script type="text/html" id="answer-view">
<div class="answer">
<div class="answer-stats {{= shared.htmlEncode(Site.toLowerCase().replace(/ /g, '')) }}">
<div class="score">
<strong>{{= shared.htmlEncode(Score) }}</strong>
<div class="votecount">votes</div>
</div>
<img class="answer-logo" src="{{= shared.htmlEncode(FaviconUrl) }}" />
</div>
<div class="answer-content">
<span class="q">Q:</span>
<div class="answer-top">
<a class="answer-title" href="{{= shared.htmlEncode(AnswerLink) }}">{{= shared.htmlEncode(Title) }}</a><br />
</div>
<span class="a">A:</span><div class="answer-body">{{= Body }}</div>
<div class="more-button" style="text-align:center; clear:both; display:none;"><a class="more">More</a></div>
</div>
</div>
</script>
HTML 标签如何在 script 标签内工作?和/或这些 html 标签和脚本标签内的类似模板代码 {{= .... }}
使用了哪种技术?
For example, view-source at Joel Spolsky's public career profile
<script type="text/html" id="stackexchangeanswerswidget">
<h3>Top Answers</h3>
<div class="answers">
</div>
</script>
<script type="text/html" id="topanswer">
<div class="top-answer">
<div class="top-answer-stats">{{= shared.htmlEncode(Score) }}</div>
<span class="top-answer-title"><a href="{{=AnswerLink}}">{{= shared.htmlEncode(Title) }}</a></span>
<a class="add-answer">add</a>
<br class="clear" />
</div>
</script>
<script type="text/html" id="answer-view">
<div class="answer">
<div class="answer-stats {{= shared.htmlEncode(Site.toLowerCase().replace(/ /g, '')) }}">
<div class="score">
<strong>{{= shared.htmlEncode(Score) }}</strong>
<div class="votecount">votes</div>
</div>
<img class="answer-logo" src="{{= shared.htmlEncode(FaviconUrl) }}" />
</div>
<div class="answer-content">
<span class="q">Q:</span>
<div class="answer-top">
<a class="answer-title" href="{{= shared.htmlEncode(AnswerLink) }}">{{= shared.htmlEncode(Title) }}</a><br />
</div>
<span class="a">A:</span><div class="answer-body">{{= Body }}</div>
<div class="more-button" style="text-align:center; clear:both; display:none;"><a class="more">More</a></div>
</div>
</div>
</script>
How does HTML tags work inside script tag? and/or What kind of technology used for those html tags, and template alike codes {{= .... }}
inside script tags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将任何您想要的内容放入
标记中。如果您指定 Javascript(或浏览器可以理解的其他脚本语言)以外的内容类型,则浏览器不会解释它,您只能以纯文本形式访问它。由于
标记的内容被视为 CDATA,因此不会解析内容,并且您可以在内容中存储不带引号的 XML 或 HTML(只要您不放置
标记,因为这将关闭您的元素)。
例如,它在 SVG Web 中使用,这是一个允许您使用内联 SVG 的填充程序HTML 格式,并在支持它的浏览器中将其转换为原生 SVG,或者在不支持它的浏览器中将其转换为 Flash。它允许通过将 SVG 包装在
标记中,将 SVG 嵌入到本身不支持 SVG 的浏览器中,这样浏览器就不会尝试将其解析为 HTML,但会失败。
对于 SO 职业者来说,他们似乎存储了与 Backbone.js 和 Underscore.js 位于
标记中,因为这是一个方便粘贴的位置HTML 中的模板。这是他们获取模板并将其提供给 Underscore 模板引擎的代码片段:
You can put anything that you want in a
<script>
tag. If you specify a content type other than Javascript (or another scripting language that the browser understands), it will not be interpreted by the browser, and you can just access it as plain text. Since the contents of<script>
tags are treated as CDATA, the contents are not parsed and you can store unquoted XML or HTML in the contents (as long as you don't ever put a</script>
tag in the contents, since that will close your element).This is used, for example, in SVG Web, a polyfill that allows you to use inline SVG in HTML and have that converted to native SVG in browsers that support it, or Flash in browsers that don't. It allows embedding of SVG in browsers that don't support it natively by wrapping it in a
<script>
tag, so the browser doesn't try and fail to parse it as HTML.In the case of SO carreers, it looks like they storing templates for use with Backbone.js and Underscore.js in
<script>
tags, since that's a convenient place to stick templates in HTML. Here's a snippet of their code where they grab the template and provide it to the Underscore template engine:关键在于脚本的 type 属性。通过将其设置为 type="text/html",它不会由浏览器的 JavaScript 引擎运行。相反,它用于其他用途,例如模板。此示例似乎使用这些标签作为模板。
请观看此 MIX 2011 网络广播,其中展示了如何在 Knockout.js 中使用类似的内容:
http://channel9.msdn。 com/events/mix/mix11/FRM08
The key is in the script's type attribute. By setting it to type="text/html" it doesn't get run by the browser's JavaScript engine. Instead it is used for other things, such as templating. This example appears to be using these tags for templates.
Check out this MIX 2011 webcast that shows how something similar is used in Knockout.js:
http://channel9.msdn.com/events/mix/mix11/FRM08