你真的需要指定类型属性吗?

2023-07-31前端开发问题
2

本文介绍了你真的需要指定类型属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

可能重复:
为什么写<脚本类型=“text/javascript”>什么时候服务器设置mime类型?

我不久前阅读了Dive into HTML5,最近又阅读了它的语义章节.我注意到它建议 notscriptstyle 上使用 type="..." 属性,因为:

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 是默认设置,
  • 浏览器不在乎.

但是,我认为在 script 中包含 type 属性(或者,恐怖,language)仍然是常见的做法>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?

编辑:这明确是关于 HTML5,而不是 XHTML.

EDIT: This is explicitly about HTML5, not XHTML.

推荐答案

大多数人习惯于 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.

对于 脚本 标签,默认为 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——那些不支持 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.

这篇关于你真的需要指定类型属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125