是否有必要编写HEAD,BODY和HTML标签? [英] Is it necessary to write HEAD, BODY and HTML tags?

查看:159
本文介绍了是否有必要编写HEAD,BODY和HTML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否需要编写< html> < head> < body> tags?



例如,我可以制作这样一个页面:

 <!DOCTYPE html> 
< meta http-equiv =Content-typecontent =text / html; charset = utf-8>
< title>页面标题< / title>
< link rel =stylesheettype =text / csshref =css / reset.css>
< script src =js / head_script.js>< / script><! - 此脚本将在头部// - >


< div>一些html< / div> <! - 这里正文开始// - >

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

Firebug正确地分离了头部和身体:



W3C验证表明它是有效的。



但我很少在网络上看到这种做法。



是否有任何理由写这些标签? 省略 html body 标签肯定是HTML规范允许的。其根本原因在于浏览器一直试图与现有的网页保持一致,而早期的HTML版本并未定义这些元素。当HTML 2.0 首次执行时,它会以缺失时推断标签的方式完成。



我经常发现省略原型设计时使用的标签,尤其是编写测试用例时的标签,因为它有助于将标记集中在有问题的测试上。推理过程完全按照您在Firebug中看到的方式创建元素,而浏览器在这方面的表现非常一致。



但是。 ..



IE在这方面至少有一个已知的错误。即使IE9展出了这一点。假设标记是这样的:

 <!DOCTYPE html> 
< title>测试用例< / title>
< form action ='#'>
< input name =var1>
< / form>

您应该(并在其他浏览器中)获取如下所示的DOM:

  HTML 
HEAD
TITLE
BODY
FORM action =#
INPUT name =var1

但是在IE中你得到这个:

  HTML 
HEAD
TITLE
FORM action =#
BODY
INPUT name = var1
BODY

亲自查看



这个错误似乎局限于任何文本内容之前的表单开始标签和任何主体开始标记。


Is it necessary to write <html>, <head> and <body> tags?

For example, I can make such a page:

<!DOCTYPE html>     
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <script src="js/head_script.js"></script><!-- this script will be in head //-->


<div>Some html</div> <!-- here body starts //-->

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

And Firebug correctly separates head and body:

W3C Validation says it's valid.

But I rarely see this practice on the web.

Are there any reason to write these tags?

解决方案

Omitting the html, head, and body tags is certainly allowed by the HTML specs. The underlying reason is that browsers have always sought to be consistent with existing web pages, and the very early versions of HTML didn't define those elements. When HTML 2.0 first did, it was done in a way that the tags would be inferred when missing.

I often find it convenient to omit the tags when prototyping and especially when writing test cases as it helps keep the mark-up focused on the test in question. The inference process should create the elements in exactly the manner that you see in Firebug, and browsers are pretty consistent in doing that.

But...

IE has at least one known bug in this area. Even IE9 exhibits this. Suppose the markup is this:

<!DOCTYPE html>
<title>Test case</title>
<form action='#'>
   <input name="var1">
</form>

You should (and do in other browsers) get a DOM that looks like this:

HTML
    HEAD
        TITLE
    BODY
        FORM action="#"
            INPUT name="var1"

But in IE you get this:

HTML
    HEAD
       TITLE
       FORM action="#"
           BODY
               INPUT name="var1"
    BODY

See it for yourself.

This bug seems limited to the form start tag preceding any text content and any body start tag.

这篇关于是否有必要编写HEAD,BODY和HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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