问题描述
在HTML5中,元素可以具有存储在XML属性中的任意元数据,这些XML属性的名称以data-
开头,例如<p data-myid="123456">
.这也是SVG规范的一部分吗?
In HTML5, elements can have arbitrary metadata stored in XML attributes whose names start with data-
such as <p data-myid="123456">
. Is this part of the SVG spec too?
在实践中,此技术在许多地方都适用于SVG文档.但是我想知道它是否属于官方SVG规范,因为该格式还很年轻,以至于浏览器之间仍然存在很多不兼容,尤其是在移动设备中.因此,在提交代码之前,我想知道我是否可以期望将来的浏览器能够支持此功能.
In practice this technique works fine for SVG docs in many places. But I'd like to know if it's part of the official SVG spec or not, because the format is young enough that there's still a lot of incompatibility between browsers, especially in mobile. So before committing to code I'd like know if I can expect future browsers to converge on supporting this.
我从工作组邮件列表中找到了此消息,说他们期望[他们]会支持它.这正式了吗?
I found this message from the working group mailing list saying they "expect [they] will" support it. Did this become official?
推荐答案
虽然其他答案在技术上是正确的,但它们忽略了SVG为data-*
提供替代机制的事实. SVG 允许包含任何属性和标签,只要不包含不会与现有的冲突(换句话说:您应该使用名称空间).
While other answers are technically correct, they omit the fact that SVG provides an alternative mechanism for data-*
. SVG allows any attribute and tag to be included, as long as it doesn't conflict with existing ones (in other words: you should use namespaces).
要使用这种(等效)机制,请执行以下操作:
To use this (equivalent) mechanism:
- 使用
mydata:id
而不是data-myid
,例如:<p mydata:id="123456">
- 确保在SVG开头标记中定义名称空间,如下所示:
<svg xmlns:mydata="http://www.myexample.com/whatever">
- use
mydata:id
instead ofdata-myid
, like this:<p mydata:id="123456">
- make sure you define the namespace in SVG opening tag, like this:
<svg xmlns:mydata="http://www.myexample.com/whatever">
编辑: SVG2 ,目前是W3C候选推荐书(04 2018年10月),将直接支持data-
(不使用命名空间,与HTML相同).但是,要获得广泛支持,还需要一些时间.感谢@cvrebert 指出这一点.
SVG2, currently W3C Candidate Recommendation (04 October 2018), will support data-
directly (without namespaces, the same as HTML). It will take some time before the support is widespread though. Thanks @cvrebert for pointing this out.
这篇关于SVG文档是否支持自定义数据属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!