SVG 文档是否支持自定义数据属性? [英] Do SVG docs support custom data- attributes?

查看:56
本文介绍了SVG 文档是否支持自定义数据属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 HTML5 中,元素可以将任意元数据存储在名称以 data- 开头的 XML 属性中,例如 <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 of data-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 候选推荐 (042018 年 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 文档是否支持自定义数据属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆