什么是{%在JavaScript中的意思

pta*_*mzz 1 javascript

我正在尝试使用来自Github的这个脚本,网址为https://github.com/blueimp/jQuery-File-Upload

该脚本有js代码之类的

<script id="template-upload" type="text/html">
    {% for (var i=0, files=o.files, l=files.length, file=files[0]; i<l; file=files[++i]) { %}
        <tr class="template-upload fade">
            <td class="preview"><span class="fade"></span></td>
            <td class="name">{%=file.name%}</td>
            <td class="size">{%=o.formatFileSize(file.size)%}</td>
            {% if (file.error) { %}
                <td class="error" colspan="2"><span class="label important">Error</span> {%=fileUploadErrors[file.error] || file.error%}</td>
            {% } else if (o.files.valid && !i) { %}
                <td class="progress"><div class="progressbar"><div style="width:0%;"></div></div></td>
                <td class="start">{% if (!o.options.autoUpload) { %}<button class="btn primary">Start</button>{% } %}</td>
            {% } else { %}
                <td colspan="2"></td>
            {% } %}
            <td class="cancel">{% if (!i) { %}<button class="btn info">Cancel</button>{% } %}</td>
        </tr>
    {% } %}
    </script>
Run Code Online (Sandbox Code Playgroud)
  • 这里,是什么{%或者%}是什么意思?
  • 为什么脚本标签有id?有什么用,我以前从未见过它?
  • 脚本type="text/html"是什么意思?我一直都在使用 type="text/javascript".

Geo*_*uer 7

没什么,你不是在写javascript,你只是在写一些看起来很像javascript的东西.

注意这条线

<script id="template-upload" type="text/html">
Run Code Online (Sandbox Code Playgroud)

如果您正在编写javascript,那么类型将是text/javascript(或者在html5中,您可以省略所假设的类型).

所以回答你的三个问题:

  • 它似乎是你正在使用的模板语言的一部分意思是"评估里面的代码{% ... %},好像它是javascript."
  • id没有什么特别之处,它只是xml的一部分.与通过id(getbyId("template-upload")或"#template-upload")引用任何DOM节点的方式相同,您也可以引用此脚本块 - 它只是DOM的一部分,就像<div><input>元素是.
  • "text/html"只是说"这个块类型很可能是html",如果它是"text/javascript",浏览器只会尝试将其作为JS执行并抛出错误,这样,模板引擎就有机会抓住那个节点并做一些事情把它变成真正的html.