简体   繁体   English

HTML5中的自定义标记命名空间实现

[英]Custom tag namespace implementation in HTML5

For an audio player I'm building, I'd like to have a tag similar to what Google and Facebook use for their share widgets. 对于我正在构建的音频播放器,我希望有一个类似于Google和Facebook用于其共享小部件的标签。 For example, it could be: 例如,它可能是:

<fp:player data-type="mp3" data-href="/path/to/file.mp3" />

What's the best way to implement this custom tag, and have it be valid in as many browsers as possible? 实现此自定义标记的最佳方法是什么,并使其在尽可能多的浏览器中有效?

The above-linked specification also states, 以上链接的规范指出,

User agents must treat elements and attributes that they do not understand as semantically neutral; 用户代理必须将他们不理解的元素和属性视为语义中立; leaving them in the DOM (for DOM processors), and styling them according to CSS (for CSS processors), but not inferring any meaning from them. 将它们留在DOM中(对于DOM处理器),并根据CSS(对于CSS处理器)对它们进行样式化,但不会从它们中推断出任何含义。

Quentin's posted quote, if I understand the specification correctly, is referring to user-agent extensions. 如果我正确理解规范,Quentin发布的引用是指用户代理扩展。 (By which I assume they mean both special browsers and browser plugins.) User-agents should not create new tags or attributes. (我认为它们意味着特殊的浏览器和浏览器插件。) 用户代理不应该创建新的标签或属性。 Web developer can use whatever made-up tags and attributes they like. Web开发人员可以使用他们喜欢的任何组成标签和属性。 And the specification explicitly instructs user-agent developers to account for unknown tags and attributes, and include them in the DOM and when rendering the page. 规范明确指示用户代理开发人员考虑未知标​​记和属性,并将它们包含在DOM中以及呈现页面时。

Accessing them with script is where it becomes a little tricky. 使用脚本访问它们变得有点棘手。 IE's 7 and 8 for you to add a namespace to find those custom tags, which makes your document technically invalid -- but still fully functional! IE的7和8为您添加命名空间以查找这些自定义标记,这使您的文档在技术上无效 - 但仍然完全正常运行!

See here (my blog): http://blog.svidgen.com/2012/10/building-custom-xhtml5-tags.html 看到这里(我的博客): http//blog.svidgen.com/2012/10/building-custom-xhtml5-tags.html

From the HTML specification : HTML规范

For markup-level features that are intended for use with the HTML syntax, extensions should be limited to new attributes of the form "x-vendor-feature", where vendor is a short string that identifies the vendor responsible for the extension, and feature is the name of the feature. 对于旨在与HTML语法一起使用的标记级功能,扩展应限于“x-vendor-feature”形式的新属性,其中vendor是一个短字符串,用于标识负责扩展的供应商和功能是该功能的名称。 New element names should not be created 不应创建新元素名称

So you can't create it and conform to the specification and Facebook and Google are very, very naughty. 因此,您无法创建它并符合规范,Facebook和Google非常非常顽皮。

You appear to be trying to reinvent the <audio> element anyway. 您似乎试图重新发明<audio>元素 So just use that and the problem of extending the langage goes away. 所以只要使用它,扩展语言的问题就会消失。

observe how the html5shim or the html5shiv create new elements: https://github.com/aFarkas/html5shiv/blob/master/src/html5shiv.js 观察html5shim或html5shiv如何创建新元素: https//github.com/aFarkas/html5shiv/blob/master/src/html5shiv.js

essentially you just have to document.createElement() the element, then you can use it. 基本上你只需要document.createElement()元素,然后你就可以使用它。

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

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