HTML中的SCRIPT和STYLE元素的type属性? [英] The type attribute of SCRIPT and STYLE elements in HTML?

查看:100
本文介绍了HTML中的SCRIPT和STYLE元素的type属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说(来自Crockford)LINK和SCRIPT元素上的哪些类型属性在这些元素用于加载外部资源时是多余的。 (因为HTTP响应决定资源的内容类型。)

 < link rel =样式表href = foo.css> 

< script src =foo.js>< / script>

但是,如果将非HTML代码内联放置在STYLE和SCRIPT元素中,情况如何?

 < style> 
/ *内联CSS规则* /
< / style>

< script>
//嵌入JavaScript代码
< / script>

是否在这些情况下设置了type属性?
当我们选择省略type属性时,是否有任何问题?

类型属性对 < script> < style>


作者必须为此属性提供值;这个属性没有默认值。


据我所知,所有浏览器缺省的默认回退是 text / javascript text / css ,分别。这是普遍的(虽然无效)做法不使用类型属性。我仍然会指定它以避免浏览器问题。

HTML 5接受现实并将这些值声明为 < style> < script> 。我非常肯定,这可以将它们关闭以保留内联内容,并自动使用正确的内容类型进行分析。


I heard (from Crockford) what type attributes on LINK and SCRIPT elements are superfluous when those elements are used to load external resources. (Because the HTTP response determines the content-type of the resource.)

<link rel="Stylesheet" href="foo.css">

<script src="foo.js"></script>

But what about the case when non-HTML code is placed inline inside the STYLE and SCRIPT elements?

<style>
    /* inline CSS rules */
</style>

<script>
    // inline JavaScript code
</script>

Is setting the type attribute in those cases recommended? Are there any issues when we choose to omit the type attribute?

解决方案

For HTML 4, the answer is simple: The type attribute is required for both <script> and <style>.

Authors must supply a value for this attribute; there is no default value for this attribute.

As far as I know, the default fallback in all browsers in its absence is text/javascript and text/css, respectively. It's widespread (though invalid) practice to not use the type attribute. I would still specify it to avoid browser problems.

HTML 5 accepts reality and declares these values as official defaults for <style> and <script>. I'm pretty sure this makes it okay to leave them off for inline content as well, which will be parsed using the correct content type automatically.

这篇关于HTML中的SCRIPT和STYLE元素的type属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆