主题
  • 默认模式
  • 浅蓝色模式
  • 淡红色模式
  • 深夜模式

HTML5 浏览器支持

所有现代浏览器都支持 HTML5。

此外,所有浏览器,不论新旧,都会自动把未识别元素当做行内元素来处理。

正因如此,您可以帮助老式浏览器处理 "未知的" HTML 元素。

注释:您甚至可以教会石器时代的 IE6 如何处理未知的 HTML 元素。


把 HTML5 元素定义为块级元素

HTML5 定义了八个新的语义 HTML 元素,所有都是块级元素

您可以把 CSS display 属性设置为 block,以确保老式浏览器中正确的行为:

header, section, footer, aside, nav, main, article, figure {
    display: block;
}

向 HTML 添加新元素

您可以通过浏览器 trick 向 HTML 添加任何新元素:

本例向 HTML 添加了一个名为<myHero>的新元素,并为其定义 display 样式:

实例代码 运行代码
复制
<!DOCTYPE html>
<html>
<head>
    <title>Creating an HTML Element</title>
    <script>document.createElement("myHero")</script>
    <style>
        myHero {
            display: block;
            background-color: #ddd;
            padding: 50px;
            font-size: 30px;
        }
    </style>
</head>
<body>
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
    <myHero>My First Hero</myHero>
</body>
</html>

已添加的 JavaScript 语句 document.createElement("myHero"),仅适用于 IE。


Internet Explorer 的问题

上述方案可用于所有新的 HTML5 元素,但是:

注意:Internet Explorer 8 以及更早的版本,不允许对未知元素添加样式。

幸运的是,Sjoerd Visscher 创造了 "HTML5 Enabling JavaScript", "the shiv":

<!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

以上代码是一段注释,但是 IE9 的早期版本会读取它(并理解它)。


完整的 Shiv 解决方案

HTML5 Shiv(也称为 html5shiv)是一个用于让旧版 Internet Explorer(IE 6/7/8)支持 HTML5 新元素的 JavaScript 解决方案。

实例代码 运行代码
复制
<!DOCTYPE html>
<html>
<head>
    <title>Styling HTML5</title>
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
    <h1>My First Article</h1>
    <article>
        London is the capital city of England.
        It is the most populous city in the United Kingdom,
        with a metropolitan area of over 13 million inhabitants.
    </article>
</body>
</html>



评论区 0
发表评论