Do SVG docs support custom data- attributes?

前端 未结 4 835
挽巷
挽巷 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 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:

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

提交回复
热议问题