简体   繁体   English

测试 HTML5 标签、事件和属性支持

[英]Tests for HTML5 tags, events, and attributes support

Does anyone have a foolproof way to test for the existence of an HTML5 tag, events, or attribute?有没有人有一种万无一失的方法来测试 HTML5 标记、事件或属性是否存在? What I would essentially like to do is browser test one or more of these.我本质上想做的是浏览器测试其中的一个或多个。 I am well aware of projects like modernizr, however I need something very specific.我很了解像 Modernizr 这样的项目,但是我需要一些非常具体的东西。

For example, Javascript which provided capability for these mocked functions would be ideal:例如,为这些模拟函数提供功能的 Javascript 将是理想的:

bool IsHTML5TagSupported( tag )
bool IsHTML5EventSupported( event )
bool IsHTML5AttributeSupported( attribute );

UPDATE: Some good reference here http://diveintohtml5.ep.io/everything.html , but by no means complete coverage of tags such as article, nav, header, etc. as well as events and all attributes.更新:这里有一些很好的参考http://diveintohtml5.ep.io/everything.html ,但绝不是对文章、导航、标题等标签以及事件和所有属性的完整覆盖。

Modernizr has a function for isEventSupported and testing for attributes is quite straightforward (have a look at the first test in function webforms() in the modernizr code). Modernizr有一个用于isEventSupported的函数,并且对属性的测试非常简单(查看isEventSupported代码中function webforms()中的第一个测试)。

When it comes to supporting tags 'such as article, nav, header, etc' I doubt you really need to test for that.当涉及到支持标签“例如文章、导航、标题等”时,我怀疑您是否真的需要对此进行测试。 If we leave aside IE for the moment, the difference between a browser which supports article and a browser which doesn't is that the former will provide some default CSS for the element.如果我们暂时撇开 IE 不谈,支持article的浏览器和不支持article的浏览器的区别在于前者会为元素提供一些默认的 CSS。 Since you can just provide your own CSS ( display: block; plus whatever styles you were going to provide anyway) it's hard to see where you might need to detect the support.由于您可以只提供您自己的 CSS( display: block;加上您无论如何要提供的任何样式),因此很难看出您可能需要检测支持的位置。

If you're still keen to test for support for these elements you can just look for those default styles, for example:如果您仍然热衷于测试对这些元素的支持,您可以只查找这些默认样式,例如:

function IsHTML5TagSupported(tag) {
    var t = document.createElement(tag);
    document.body.appendChild(t);
    var ret;
    if (window.getComputedStyle) ret = window.getComputedStyle(t, null).getPropertyValue("display") == 'block';
    if (t.currentStyle) ret = t.currentStyle.display == 'block';
    document.body.removeChild(t);
    return ret;
}

window.alert(IsHTML5TagSupported('article'));

You need to actually append the element for the browser to compute the styles, and I've added the currentStyle line for IE even though we can be sure it doesn't support it.您需要为浏览器实际附加元素以计算样式,并且我已经为 IE 添加了currentStyle行,即使我们可以确定它不支持它。 Obviously this particular approach is only going to work for elements that are supposed to be block level, but you could extend it to look at a different property for the rest.显然,这种特殊方法仅适用于应该是块级的元素,但您可以扩展它以查看其余元素的不同属性。 Of course, calling document.createElement('article') will cause IE to apply user styles to subsequent article elements and having article { display: block; }当然,调用document.createElement('article')会导致 IE 将用户样式应用到后续的article元素,并让article { display: block; } article { display: block; } in your stylesheet will cause all browsers to 'support' the article element. article { display: block; }在样式表将导致所有浏览器“支持”的article元素。

Perhaps a more interesting thing to test for is compliance with the HTML5 parsing algorithm , as that may have some impact on the DOM tree you are dealing with in script and whether you'll be able to do things like insert inline SVG .或许更有趣的测试是是否符合HTML5 解析算法,因为这可能会对您在脚本中处理的 DOM 树以及您是否能够执行诸如插入内联 SVG 之类的操作产生一些影响。 Have a look at the testParsing function in the source code of The HTML5 Test for that. testParsing请查看The HTML5 Test源代码中的testParsing函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM