javascript Modernizr 与 HTML shiv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8275580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:53:04  来源:igfitidea点击:

Modernizr vs HTML shiv

javascriptinternet-explorerhtmlbrowser

提问by federico-t

If I ONLY need older browsers to recognize HTML5 tags, which should I use, Modernizror the popular HTML5 shiv? And also, if I don't need to style this HTML5 tags, do I need the browsers to recognize them anyways? Or is it only necessary when adding CSS to these tags?

如果我只需要较旧的浏览器来识别 HTML5 标签,我应该使用Modernizr还是流行的HTML5 shiv?而且,如果我不需要设置这个 HTML5 标签的样式,我是否需要浏览器来识别它们?还是只有在向这些标签添加 CSS 时才需要?

Thanks!

谢谢!

回答by omarello

html5shiv basically allows IE to recognize and style HTML5 elements, while Modernizr provides the same plus feature detection supported by a broswer.

html5shiv 基本上允许 IE 识别 HTML5 元素并为其设置样式,而 Modernizr 提供了浏览器支持的相同功能检测。

So to answer your question HTML5 shiv should be sufficient to recognize HTML5 tags in IE. (note I say IE here since that is what the html5shiv targets, not sure what you mean when you say older browsers)

因此,要回答您的问题,HTML5 shiv 应该足以识别 IE 中的 HTML5 标签。(注意我在这里说的是 IE,因为这是 html5shiv 的目标,不确定你说的旧浏览器是什么意思)

For the second part, even if you don't want to style the HTML5 tags, I would advice to use the shiv since the browser may either display them incorrectly, or not at all, so it is better to be safe.

对于第二部分,即使您不想设置 HTML5 标签的样式,我还是建议使用 shiv,因为浏览器可能会错误地显示它们,或者根本不显示它们,因此最好是安全的。

回答by Emil

HTML5 Shiv is smaller, and unless you need some of Modernizr's detection functionality, go for the shiv.

HTML5 Shiv 更小,除非您需要一些 Modernizr 的检测功能,否则请选择 shiv。

The size of the file is very important, since it is always in the head of the html document and blocks the download of additional resources until fully executed.

文件的大小非常重要,因为它始终位于 html 文档的头部并阻止其他资源的下载,直到完全执行为止。

Since it is only needed for older browsers, I use the following code:

由于仅旧浏览器需要它,因此我使用以下代码:

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

You can remove the html tags you don't use, but then you will be unable the use the file hosted at Google.

您可以删除不使用的 html 标签,但是您将无法使用 Google 托管的文件。

回答by ElvinD

You can alternatively use Modernizr to load a particular file if browser supports that particular CSS or HTML5 features.

如果浏览器支持特定的 CSS 或 HTML5 功能,您也可以使用 Modernizr 来加载特定的文件。

Let say if the browser doesn't support 'canvas' as HTML5 element and 'fontface' as CSS3 property

假设浏览器不支持“canvas”作为 HTML5 元素和“fontface”作为 CSS3 属性

Modernizr.load({
test: Modernizr.fontface && Modernizr.canvas, // Test if the browser supports it or not
yep : '/path-to/html5-css3-attributes.css',  // If browser supports it, load this file
nope: '/path-to/old-css-attributes.css' // If NOT, load this instead
});

You can use .js also in your file path condition.

您也可以在文件路径条件中使用 .js。