你真的需要指定type属性吗? [英] Do you really need to specify the type attribute?
问题描述
可能重复:
为什么写< script type =" text / javascript&何时由服务器设置mime类型?
我阅读了一段时间内深入HTML5,最近才读到它的语义章节。我注意到建议不在脚本使用
和 type =...
属性 style
,因为:
I read Dive into HTML5 a while back, and read its semantics chapter again just recently. I noted it advises not to use type="..."
attributes on script
and style
, because:
- MIME类型应由服务器发送,
- JS和CSS是默认值,
- 浏览器不在乎。
但是,我认为仍然通常的做法是包括 type
属性(或者,恐怖,语言
)脚本
和 style
标签。假设服务器被正确配置为发送正确的MIME类型,是否有理由使用非显式的这些?
However, I see it is still common practice to include type
attributes (or, horror, language
) on both script
and style
tags. Assuming the server is properly configured to send the correct MIME types, are there reasons for using these other than being explicit?
EDIT :这是
推荐答案
大多数人习惯于HTML 4 / XHTML和之前,其中 type
属性是必需的。
Most people are used to HTML 4/XHTML and before, where the type
attribute is required for these elements.
对于HTML 5,这些都是可选的,
In regards to HTML 5, these are indeed optional and the spec gives a default, depending on the element.
对于 script
标签,默认为 text / javascript
:
如果语言不是由text / javascript描述的,那么type属性必须存在
If the language is not that described by "text/javascript", then the type attribute must be present
对于 style
标签,默认为 text / css
:
如果属性缺失,则使用type属性的默认值为text / css。
The default value for the type attribute, which is used if the attribute is absent, is "text/css".
所以,不需要,正如你所说的。但是,浏览器支持和服务器设置并不总是可以依赖的 - 显式是一个好主意,因为它避免了这样的问题。
So, not needed, as you stated. However, browser support and server setups can't always be relied on - being explicit is a good idea as it avoids such problems.
当然,并不是所有的浏览器支持HTML 5 - 那些不会使用早期版本的属性是必需的,并且您的javascript / css可能无法在这样的浏览器中解析,这意味着最终没有CSS或javascript在旧版的浏览器,当一个简单的解决方案向后兼容性是添加属性。
And of course, not all browsers out there support HTML 5 - those that don't will use an earlier version where the attribute is required and your javascript/css might not get parsed in such browsers, meaning you end up with no CSS or javascript on older browsers, when a simple solution for backwards compatibility is to add the attribute.
这篇关于你真的需要指定type属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!