。这也是 SVG 规范的一部分吗?在实践中,这种技术在很多地方都适用于 SVG 文档。但是我想知道它是否是官方 SVG 规范的一部分,因为该格式足够年轻,浏览器之间仍然存在很多不兼容,尤其是在移动设备中。因此,在提交代码之前,我想知道我是否可以期待......" /> 。这也是 SVG 规范的一部分吗?在实践中,这种技术在很多地方都适用于 SVG 文档。但是我想知道它是否是官方 SVG 规范的一部分,因为该格式足够年轻,浏览器之间仍然存在很多不兼容,尤其是在移动设备中。因此,在提交代码之前,我想知道我是否可以期待......"> 。这也是 SVG 规范的一部分吗?在实践中,这种技术在很多地方都适用于 SVG 文档。但是我想知道它是否是官方 SVG 规范的一部分,因为该格式足够年轻,浏览器之间仍然存在很多不兼容,尤其是在移动设备中。因此,在提交代码之前,我想知道我是否可以期待......" />
ChatGPT解决这个技术问题 Extra ChatGPT

Do SVG docs support custom data- attributes?

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?

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?


j
johndodo

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:

use mydata:id instead of data-myid, like this:

make sure you define the namespace in SVG opening tag, like this:

EDIT: 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.


Third part of the equation: el.getAttribute('mydata:id') to get the data you attached to the SVG element. (Note: if you're using d3, the namespace will be stripped by default and you'll just el.getAttribute('id').)
This should be the accepted answer. SVG is an extension of XML which allows you to use tags from different namespaces.
Why does the namespace have to be custom? Why wouldn't declaring an HTML5 namespace in the document be enough to use data-* in SVG?
FYI, whether using a private namespace (e.g.: <svg xmlns="http://www.w3.org/2000/svg" xmlns:mydata="http://www.myexample.com/whatever"><text x="10" y="20" mydata:id="something">SVG</text></svg>) or the xhtml namespace, neither will validate on validator.w3.org/check (using SVG 1.1), but both do work in the browser. It's then possible to use either getAttribute or getAttributeNS to fetch the data.
Is there a way to make the validator happy? Does the response from "myexample.com/whatever" have to be some specific content?
u
unor

The data-* attribute is part of HTML5. It’s not a generic XML attribute.

The current SVG W3C Recommendation is SVG 1.1 (from 2011-08). It doesn’t allow this attribute, as you can check in the attributes list.

The same is the case for the SVG 2 Working Draft (from 2012-08). Update (2015): It seems that it’s intended to support data-* attributes in SVG 2 (currently still a Working Draft).


c
cvrebert

data-* attributes on SVG elements are officially supported in the current draft of SVG2. See:

w3c/svgwg commit 1cb4ee9: Added SVGElement.dataset and allowed data-* attributes on all SVG elements.

ACTION-3694: Add "data-*" attributes notes to spec. (Created on: January 15, 2015)

RESOLUTION: We will reserve "data-*" attributes to be used in SVG content. The API for handling them is on Element. (from SVG WG Telecon on 15-Jan-2015)

https://lists.w3.org/Archives/Public/www-svg/2014Dec/0022.html


c
collapsar

there is a more general mechanism.

svg supports desc elements which may contain arbitrary xml from other namespaces. link instances of this elements or child nodes from you own namespace by dependent ids or refid attributes.

this is the relevant part of the spec (5.4).


Thanks for the pointer. Should I infer SVG doesn't officially support data- attributes?
Isn't desc meant for accessibility sakes?
@matt I don't think so, at least based On the stabndard.
@matt Not necessarily. Afaik the standard would only mention the purpose of annotation independent of any rendering. This doesn't contradict the element's suitability for the purpose. Specifically, there is a blog post discussing the use of aria-labelledby attributes and desc elements as accessible labels. MDN recommends a similar use. given the plethora of google results, best practices for accessible svg might be worth a question of its own,
@RockyRoad: not really - SVG specification explicitly allows such attributes (as opposed to for instance HTML/XHTML which doesn't). Also note that while you can (mis)use description elements for arbitrary data it is (IMHO) quite obvious from the link that this was not the intended purpose of the desc element. Not saying you shouldn't do it, just that there is a better way.