【问题标题】:what does browser do when it encounters <script type= text/template ..> [duplicate]浏览器遇到 <script type= text/template ..> 时会做什么 [重复]
【发布时间】:2012-10-30 17:00:56
【问题描述】:

可能重复:
Explanation of <script type = “text/template”> … </script>

在开始学习backbone.js之后,我想知道浏览器遇到时到底会做什么

<script type= text/template>
 ...
</script>

http://bytes.com/topic/html-css/answers/542484-browser-behavior-unknown-tags-attributes 人们正在解释如果看到未知标签会发生什么,但在这种情况下标签是已知的并且浏览器知道如何处理它,问题在于类型。

所以问题是:

  • 它会尝试执行它吗?
  • 是忽略了吗?

为什么我们需要&lt;script type= text/template&gt;...&lt;/script&gt;,为什么我们不能使用&lt;div style="display:none"&gt; ... &lt;/div&gt;

【问题讨论】:

  • HTML 元素信息的最佳起点是 HTML 规范,谷歌搜索“site:w3.org html the-element-you-want”,将搜索限制在 w3.org 以避免垃圾像 w3fools.com。阅读&lt;script&gt;w3.org/TR/html-markup/script.html

标签: javascript backbone.js


【解决方案1】:

浏览器会忽略它——它无法尝试执行脚本,因为它无法识别类型。

而且您不能使用 div,因为模板通常不是有效的 HTML(例如,下划线模板语法 &lt;%= modelproperty %&gt;

【讨论】:

    【解决方案2】:

    浏览器会简单地忽略这些标签。 将模板放在这样的标签内很有用,因为您不需要编码 HTML 符号,例如 &lt;&gt;&amp;

    <script type="text/template">
        <% for(var i = 0; i < 10; i++) { %>
            // do something
        <% } %>
    </script>
    

    在隐藏的 div 中,你应该这样写:

    <script type="text/template">
        &lt;% for(var i = 0; i &lt; 10; i++) { %&gt;
            // do something
        &lt;% } %&gt;
    </script>
    

    【讨论】:

      猜你喜欢
      • 2011-06-22
      • 2014-11-02
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2023-03-26
      • 2012-07-28
      相关资源
      最近更新 更多