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

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

问题描述

是否需要写<html><head><body>标签?

比如我可以做这样一个页面:

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>

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

And Firebug correctly separates head and body:

W3C 验证表明它是有效的.

W3C Validation says it's valid.

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

But I rarely see this practice on the web.

有什么理由写这些标签吗?

Are there any reason to write these tags?

推荐答案

省略 htmlheadbody 标签 当然是 HTML 规范允许的.根本原因是浏览器一直在寻求与现有网页保持一致,而 HTML 的早期版本并没有定义这些元素.当 HTML 2.0 第一次这样做时,它是以一种在缺少标签时会被推断出来的方式完成的.

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.

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

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.

但是……

IE 在这方面至少有一个已知错误.甚至 IE9 也展示了这一点.假设标记是这样的:

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>

你应该(并且在其他浏览器中这样做)得到一个如下所示的 DOM:

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

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

但是在 IE 中你会得到这个:

But in IE you get this:

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

自己看吧.

此错误似乎仅限于任何文本内容和任何 body 开始标记之前的 form 开始标记.

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

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

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