Do SVG docs support custom data- attributes?

前端 未结 4 738
挽巷
挽巷 2020-11-30 01:53

In HTML5, elements can have arbitrary metadata stored in XML attributes whose names start with data- such as

. Is th

相关标签:
4条回答
  • 2020-11-30 01:58

    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
    0 讨论(0)
  • 2020-11-30 02:12

    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: <p mydata:id="123456">
    • make sure you define the namespace in SVG opening tag, like this: <svg xmlns:mydata="http://www.myexample.com/whatever">

    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.

    0 讨论(0)
  • 2020-11-30 02:18

    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).

    0 讨论(0)
  • 2020-11-30 02:20

    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).

    0 讨论(0)
提交回复
热议问题