Javascript script type="text/html" 的现代用途是什么,这个例子被认为是很好的用途吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13883553/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:02:18  来源:igfitidea点击:

What are modern uses of script type="text/html" and is this example considered good use?

javascriptcsshtmltemplates

提问by Shane

Is something like...

是不是有点像...

<script type="text/html" id="this-content1">
<h1>This Header Info One</h1>
<p>This content one. . .</p>
</script>
<script type="text/html" id="this-content2">
<h1>This Header Info Two</h1>
<p>This content two. . .</p>
</script>

...and using jQuery to swap out the content based on a selector good practice in today's standards?

...并使用 jQuery 根据当今标准中的选择器良好实践来换出内容?

I am just getting into the use of script type="text/html"...to allow dynamic changes of my content and am finding many ways to do this. Is there a source that might explain the direction this is going and if any standardizing of this practice.

我刚刚开始使用script type="text/html"...来允许动态更改我的内容,并且我正在寻找很多方法来做到这一点。是否有来源可以解释这种做法的发展方向,以及这种做法是否有任何标准化。

I see code like...

我看到像这样的代码...

<div class="thumbnail">
            <# if ( data.uploading ) { #>
                <div class="media-progress-bar"><div></div></div>
            <# } else if ( 'image' === data.type ) { #>
                <img src="{{ data.size.url }}" draggable="false" />
            <# } else { #>
                <img src="{{ data.icon }}" class="icon" draggable="false" />
            <# } #>
        </div>

...nested in a script type="text/html" tag and really have no idea why it is written this way. Also have just wet my beak in backbone and this looks to be a little heavy if just looking to add content swapping in one page.

...嵌套在 script type="text/html" 标签中,真的不知道为什么它是这样写的。也刚刚在骨干中弄湿了我的喙,如果只是想在一页中添加内容交换,这看起来有点沉重。

采纳答案by wprl

According to the HTML5 spec for the script tag, it's totally fine to use <script>with a typeattribute set to any valid MIME type. That includes MIME types like text/htmlor text/plain.

根据脚本标签HTML5 规范,使用设置为任何有效 MIME 类型<script>type属性完全没问题。这包括 MIME 类型,如text/htmltext/plain

According to the HTML4 spec for the script tag, it's not quite fine:

根据脚本标签HTML4 规范,它不太好:

"There are two types of scripts authors may attach to an HTML document: Those that are executed one time when the document is loaded [and t]hose that are executed every time a specific event occurs"

“有两种类型的脚本作者可以附加到 HTML 文档:一种是在加载文档时执行一次 [和 t] 每次发生特定事件时都会执行的脚本”

You don't need backbone for templating. You can use e.g. jQuery or my personal favorite, Mustache.js.

您不需要模板的主干。您可以使用例如 jQuery 或我个人最喜欢的Mustache.js

回答by Jonathan Ong

I'm assuming you want to save a portion of HTML to use later. Putting non-script data in a script tag does not make sense. Do what Facebook does!

我假设您想保存一部分 HTML 以供以后使用。将非脚本数据放在脚本标签中是没有意义的。做 Facebook 所做的!

<code class="hide" id="code1"><!--
  <p>My HTML here</p>
  <script>My Javascript here</script>
--></code>

Then you can grab the HTML later and do whatever you want later:

然后你可以稍后抓取 HTML 并在以后做任何你想做的事情:

var html = document.querySelector('#code1').innerText.slice(5, -5)

The scripts inside won't be executed until you handle them properly.

在您正确处理它们之前,不会执行里面的脚本。

Some notes:

一些注意事项:

  • No idea what the differences between innerText and other text functions are
  • I don't think you can just insert script tags into the DOM. Not sure how jQuery does it
  • 不知道innerText和其他文本函数有什么区别
  • 我认为您不能将脚本标签插入到 DOM 中。不知道 jQuery 是怎么做的