HTML脚本标记:类型或语言(或省略两者)?

编程入门 行业动态 更新时间:2024-10-25 12:29:27
本文介绍了HTML脚本标记:类型或语言(或省略两者)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

< script type =text / javascript> / * ... * / < / script>

vs。

< script language =Javascript> / * ... * / < / script>

应该使用哪一个?为什么?

或者,第三个选择:省略其中的任何一个,例如jQuery API参考中的示例代码:

< script src = code.jquery/jquery-latest.js >< /脚本>

解决方案

语言属性已被弃用很长一段时间,并且不应该被使用。

当W3C使用HTML5时,他们发现所有浏览器都有text / javascript 作为默认脚本键入,所以他们将其标准化为默认值。因此,您不需要 type 。

对于XHTML 1.0或HTML 4.01中的页面,省略 type 被认为是无效的。尝试验证以下内容:

<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENwww.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ; < html xmlns =www.w3/1999/xhtmlxml:lang =enlang =en> < head> < script src =example/test.js>< / script> < / head> < body /> < / html>

您将收到以下错误消息:

第4行,第41列:required属性type未指定

重新成为标准的粉丝,使用它。它应该没有实际效果,但是,如果有疑问,不妨按规格行事。

<script type="text/javascript"> /* ... */ </script>

vs.

<script language="Javascript"> /* ... */ </script>

Which should be used and why?

Or, the third alternative: omitting either of these, such as the example code in jQuery's API reference:

<script src="code.jquery/jquery-latest.js"></script>

解决方案

The language attribute has been deprecated for a long time, and should not be used.

When W3C was working on HTML5, they discovered all browsers have "text/javascript" as the default script type, so they standardized it to be the default value. Hence, you don't need type either.

For pages in XHTML 1.0 or HTML 4.01 omitting type is considered invalid. Try validating the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3/1999/xhtml" xml:lang="en" lang="en"> <head> <script src="example/test.js"></script> </head> <body/> </html>

You will be informed of the following error:

Line 4, Column 41: required attribute "type" not specified

So if you're a fan of standards, use it. It should have no practical effect, but, when in doubt, may as well go by the spec.

更多推荐

HTML脚本标记:类型或语言(或省略两者)?

本文发布于:2023-10-21 17:21:59,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   标记   语言   类型   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!